Skip to content

Commit f06f874

Browse files
authored
Clarify how augmentations affect existing compile-time errors. (#4545)
Clarify how augmentations affect existing compile-time errors. Fix #3690.
1 parent 027f3ed commit f06f874

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

working/augmentations/feature-specification.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Author: rnystrom@google.com, jakemac@google.com, lrn@google.com
44

5-
Version: 1.38 (see [Changelog](#Changelog) at end)
5+
Version: 1.40 (see [Changelog](#Changelog) at end)
66

77
Experiment flag: augmentations
88

@@ -1003,6 +1003,57 @@ the combined declaration.*
10031003

10041004
[analyzer package]: https://pub.dev/packages/analyzer
10051005

1006+
### Compile errors with augmentations
1007+
1008+
Prior to augmentations, the definition of a semantic entity is produced by a
1009+
single syntactic declaration. That allows the language specification to refer to
1010+
those entities interchangeably. With augmentations, that is no longer the case.
1011+
A single semantic entity may be the product of multiple syntactic declarations
1012+
(an introductory and any number of augmentations). This raises the question of
1013+
whether existing compile errors apply to syntactic declarations or semantic
1014+
definitions. For example, the specification says:
1015+
1016+
> It is a compile-time error if two elements in the type list of the
1017+
> `implements` clause of a class `C` specifies the same type `T`.
1018+
1019+
Thus this is an error:
1020+
1021+
```dart
1022+
class C implements I, I {}
1023+
```
1024+
1025+
But what about:
1026+
1027+
```dart
1028+
class C implements I {}
1029+
1030+
augment class C implements I {}
1031+
```
1032+
1033+
Each syntactic declaration of `C` only mentions the interface once. But the
1034+
resulting definition produced by applying augmentations has an `implements`
1035+
clause with `I` in it twice. To which entity does the error apply?
1036+
1037+
The general rule is that **compile-time errors apply to semantic definitions
1038+
whenever possible.** In other words, if the library is syntactically well-formed
1039+
enough that augmentations *can* be applied, then they should be. And if doing so
1040+
eliminates what would otherwise be a compile-time error, then that error should
1041+
not be reported.
1042+
1043+
In the example above, there is an error because the resulting definition does
1044+
have the same interface twice in the `implements` clause. *(Though note that
1045+
[#4542](https://github.com/dart-lang/language/issues/4542) tracks whether we
1046+
want to change this specific error.)*
1047+
1048+
The motivation for this principle is that reorganizing code into or out of
1049+
augmentations shouldn't affect the errors that are reported as much as possible.
1050+
Augmentations are a syntactic tool for organizing code, but what the user cares
1051+
about -- and what static analysis should thus focus on -- is the semantics of
1052+
the resulting definitions. Also, in most cases the error relies on semantic
1053+
information that isn't even well defined for syntactic entities and is only
1054+
known from the resolved semantic definition which can't be produced without
1055+
applying augmentations.
1056+
10061057
## Dynamic semantics
10071058

10081059
The application of augmentation declarations to an augmented declaration
@@ -1216,6 +1267,11 @@ and assume the third point is always true.
12161267

12171268
## Changelog
12181269

1270+
### 1.40
1271+
1272+
* Clarify how applying augmentations interacts with compile-time errors
1273+
(#3690).
1274+
12191275
### 1.39
12201276

12211277
* Non-semantic copy editing. Remove some redundant specification. Clarify

0 commit comments

Comments
 (0)