Skip to content

Commit 83a9a0a

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[analyzer/cfe] Make sealed and mixin class errors more precise and synced between the CFE and analyzer.
Updated corresponding language tests. No new behaviour, updated test messages and location. Change-Id: Ib3c7e2d701bdfbb68757c532b945348f6cbebad0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280062 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 0c1bae3 commit 83a9a0a

36 files changed

+255
-267
lines changed

pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -960,10 +960,12 @@ Message _withArgumentsCantReadFile(Uri uri_, String string) {
960960
}
961961

962962
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
963-
const Template<Message Function(String name)> templateCantUseClassAsMixin =
964-
const Template<Message Function(String name)>(
965-
problemMessageTemplate: r"""Class '#name' can't be used as a mixin.""",
966-
withArguments: _withArgumentsCantUseClassAsMixin);
963+
const Template<
964+
Message Function(String name)> templateCantUseClassAsMixin = const Template<
965+
Message Function(String name)>(
966+
problemMessageTemplate:
967+
r"""The class '#name' can't be used as a mixin because it isn't a mixin class nor a mixin.""",
968+
withArguments: _withArgumentsCantUseClassAsMixin);
967969

968970
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
969971
const Code<Message Function(String name)> codeCantUseClassAsMixin =
@@ -975,7 +977,8 @@ Message _withArgumentsCantUseClassAsMixin(String name) {
975977
if (name.isEmpty) throw 'No name provided';
976978
name = demangleMixinApplicationName(name);
977979
return new Message(codeCantUseClassAsMixin,
978-
problemMessage: """Class '${name}' can't be used as a mixin.""",
980+
problemMessage:
981+
"""The class '${name}' can't be used as a mixin because it isn't a mixin class nor a mixin.""",
979982
arguments: {'name': name});
980983
}
981984

@@ -10914,7 +10917,7 @@ const Template<
1091410917
name)> templateSealedClassSubtypeOutsideOfLibrary = const Template<
1091510918
Message Function(String name)>(
1091610919
problemMessageTemplate:
10917-
r"""Sealed class '#name' can't be extended, implemented, or mixed in outside of its library.""",
10920+
r"""The class '#name' can't be extended, implemented, or mixed in outside of its library because it's a sealed class.""",
1091810921
withArguments: _withArgumentsSealedClassSubtypeOutsideOfLibrary);
1091910922

1092010923
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
@@ -10930,7 +10933,7 @@ Message _withArgumentsSealedClassSubtypeOutsideOfLibrary(String name) {
1093010933
name = demangleMixinApplicationName(name);
1093110934
return new Message(codeSealedClassSubtypeOutsideOfLibrary,
1093210935
problemMessage:
10933-
"""Sealed class '${name}' can't be extended, implemented, or mixed in outside of its library.""",
10936+
"""The class '${name}' can't be extended, implemented, or mixed in outside of its library because it's a sealed class.""",
1093410937
arguments: {'name': name});
1093510938
}
1093610939

@@ -10941,7 +10944,7 @@ const Template<
1094110944
name)> templateSealedMixinSubtypeOutsideOfLibrary = const Template<
1094210945
Message Function(String name)>(
1094310946
problemMessageTemplate:
10944-
r"""Sealed mixin '#name' can't be mixed in outside of its library.""",
10947+
r"""The mixin '#name' can't be mixed in outside of its library because it's a sealed mixin.""",
1094510948
withArguments: _withArgumentsSealedMixinSubtypeOutsideOfLibrary);
1094610949

1094710950
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
@@ -10957,7 +10960,7 @@ Message _withArgumentsSealedMixinSubtypeOutsideOfLibrary(String name) {
1095710960
name = demangleMixinApplicationName(name);
1095810961
return new Message(codeSealedMixinSubtypeOutsideOfLibrary,
1095910962
problemMessage:
10960-
"""Sealed mixin '${name}' can't be mixed in outside of its library.""",
10963+
"""The mixin '${name}' can't be mixed in outside of its library because it's a sealed mixin.""",
1096110964
arguments: {'name': name});
1096210965
}
1096310966

pkg/analyzer/lib/src/error/codes.g.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
415415
/// 0: the name of the class being used as a mixin
416416
static const CompileTimeErrorCode CLASS_USED_AS_MIXIN = CompileTimeErrorCode(
417417
'CLASS_USED_AS_MIXIN',
418-
"The class '{0}' can't be implicitly used as a mixin because it isn't a "
419-
"mixin class nor a mixin.",
418+
"The class '{0}' can't be used as a mixin because it isn't a mixin class "
419+
"nor a mixin.",
420420
);
421421

422422
static const CompileTimeErrorCode CONCRETE_CLASS_HAS_ENUM_SUPERINTERFACE =
@@ -4294,20 +4294,23 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
42944294
);
42954295

42964296
/// Parameters:
4297-
/// 0: the name of the supertype being extended, implemented, or mixed in
4297+
/// 0: the name of the sealed class being extended, implemented, or mixed in
42984298
static const CompileTimeErrorCode SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY =
42994299
CompileTimeErrorCode(
4300-
'SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY',
4301-
"The sealed class '{0}' can't be extended, implemented, or mixed in "
4302-
"outside of its library.",
4300+
'INVALID_USE_OF_TYPE_OUTSIDE_LIBRARY',
4301+
"The class '{0}' can't be extended, implemented, or mixed in outside of "
4302+
"its library because it's a sealed class.",
4303+
uniqueName: 'SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY',
43034304
);
43044305

43054306
/// Parameters:
4306-
/// 0: the name of the supertype being mixed in
4307+
/// 0: the name of the sealed mixin being mixed in
43074308
static const CompileTimeErrorCode SEALED_MIXIN_SUBTYPE_OUTSIDE_OF_LIBRARY =
43084309
CompileTimeErrorCode(
4309-
'SEALED_MIXIN_SUBTYPE_OUTSIDE_OF_LIBRARY',
4310-
"The sealed mixin '{0}' can't be mixed in outside of its library.",
4310+
'INVALID_USE_OF_TYPE_OUTSIDE_LIBRARY',
4311+
"The mixin '{0}' can't be mixed in outside of its library because it's a "
4312+
"sealed mixin.",
4313+
uniqueName: 'SEALED_MIXIN_SUBTYPE_OUTSIDE_OF_LIBRARY',
43114314
);
43124315

43134316
/// No parameters.

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,9 +1395,9 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
13951395
_checkForMixinWithConflictingPrivateMember(withClause, superclass);
13961396
_checkForConflictingGenerics(node);
13971397
_checkForBaseClassOrMixinImplementedOutsideOfLibrary(implementsClause);
1398-
_checkForClassUsedAsMixin(node, withClause);
1398+
_checkForClassUsedAsMixin(withClause);
13991399
_checkForSealedSupertypeOutsideOfLibrary(
1400-
node, superclass, withClause, implementsClause);
1400+
superclass, withClause, implementsClause);
14011401
if (node is ClassDeclaration) {
14021402
_checkForNoDefaultSuperConstructorImplicit(node);
14031403
}
@@ -1838,8 +1838,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
18381838
/// No error is emitted if the class being mixed in is a mixin class.
18391839
///
18401840
/// See [CompileTimeErrorCode.CLASS_USED_AS_MIXIN].
1841-
void _checkForClassUsedAsMixin(
1842-
NamedCompilationUnitMember node, WithClause? withClause) {
1841+
void _checkForClassUsedAsMixin(WithClause? withClause) {
18431842
if (withClause != null) {
18441843
for (NamedType withMixin in withClause.mixinTypes) {
18451844
final withType = withMixin.type;
@@ -1852,7 +1851,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
18521851
.isEnabled(Feature.class_modifiers)) {
18531852
errorReporter.reportErrorForNode(
18541853
CompileTimeErrorCode.CLASS_USED_AS_MIXIN,
1855-
node,
1854+
withMixin,
18561855
[withElement.name]);
18571856
}
18581857
}
@@ -4308,60 +4307,39 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
43084307
///
43094308
/// See [CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY],
43104309
/// [CompileTimeErrorCode.SEALED_MIXIN_SUBTYPE_OUTSIDE_OF_LIBRARY].
4311-
void _checkForSealedSupertypeOutsideOfLibrary(
4312-
NamedCompilationUnitMember node,
4313-
NamedType? superclass,
4314-
WithClause? withClause,
4315-
ImplementsClause? implementsClause) {
4316-
void reportSealedClassOrMixinSubtypeOutsideOfLibraryError(
4317-
ClassOrMixinElementImpl superclass) {
4318-
if (superclass.isSealed && superclass.library != _currentLibrary) {
4319-
if (superclass is MixinElementImpl) {
4320-
errorReporter.reportErrorForNode(
4321-
CompileTimeErrorCode.SEALED_MIXIN_SUBTYPE_OUTSIDE_OF_LIBRARY,
4322-
node,
4323-
[superclass.name]);
4324-
} else {
4325-
errorReporter.reportErrorForNode(
4326-
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY,
4327-
node,
4328-
[superclass.name]);
4310+
void _checkForSealedSupertypeOutsideOfLibrary(NamedType? superclass,
4311+
WithClause? withClause, ImplementsClause? implementsClause) {
4312+
void reportErrorsForSealedClassesAndMixins(List<NamedType> namedTypes) {
4313+
for (NamedType namedType in namedTypes) {
4314+
final type = namedType.type;
4315+
if (type is InterfaceType) {
4316+
final element = type.element;
4317+
if (element is ClassOrMixinElementImpl &&
4318+
element.isSealed &&
4319+
element.library != _currentLibrary) {
4320+
final ErrorCode errorCode;
4321+
if (element is MixinElementImpl) {
4322+
errorCode =
4323+
CompileTimeErrorCode.SEALED_MIXIN_SUBTYPE_OUTSIDE_OF_LIBRARY;
4324+
} else {
4325+
errorCode =
4326+
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY;
4327+
}
4328+
errorReporter
4329+
.reportErrorForNode(errorCode, namedType, [element.name]);
4330+
}
43294331
}
43304332
}
43314333
}
43324334

43334335
if (superclass != null) {
4334-
final superclassType = superclass.type;
4335-
if (superclassType is InterfaceType) {
4336-
final superclassElement = superclassType.element;
4337-
if (superclassElement is ClassElementImpl) {
4338-
reportSealedClassOrMixinSubtypeOutsideOfLibraryError(
4339-
superclassElement);
4340-
}
4341-
}
4336+
reportErrorsForSealedClassesAndMixins([superclass]);
43424337
}
43434338
if (withClause != null) {
4344-
for (NamedType withMixin in withClause.mixinTypes) {
4345-
final withType = withMixin.type;
4346-
if (withType is InterfaceType) {
4347-
final withElement = withType.element;
4348-
if (withElement is ClassOrMixinElementImpl) {
4349-
reportSealedClassOrMixinSubtypeOutsideOfLibraryError(withElement);
4350-
}
4351-
}
4352-
}
4339+
reportErrorsForSealedClassesAndMixins(withClause.mixinTypes);
43534340
}
43544341
if (implementsClause != null) {
4355-
for (NamedType interface in implementsClause.interfaces) {
4356-
final interfaceType = interface.type;
4357-
if (interfaceType is InterfaceType) {
4358-
final interfaceElement = interfaceType.element;
4359-
if (interfaceElement is ClassElementImpl) {
4360-
reportSealedClassOrMixinSubtypeOutsideOfLibraryError(
4361-
interfaceElement);
4362-
}
4363-
}
4364-
}
4342+
reportErrorsForSealedClassesAndMixins(implementsClause.interfaces);
43654343
}
43664344
}
43674345

@@ -5142,8 +5120,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
51425120
);
51435121
_checkForConflictingGenerics(node);
51445122
_checkForBaseClassOrMixinImplementedOutsideOfLibrary(implementsClause);
5145-
_checkForSealedSupertypeOutsideOfLibrary(
5146-
node, null, null, implementsClause);
5123+
_checkForSealedSupertypeOutsideOfLibrary(null, null, implementsClause);
51475124
}
51485125
}
51495126

pkg/analyzer/messages.yaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ CompileTimeErrorCode:
15981598
int y = x as int;
15991599
```
16001600
CLASS_USED_AS_MIXIN:
1601-
problemMessage: "The class '{0}' can't be implicitly used as a mixin because it isn't a mixin class nor a mixin."
1601+
problemMessage: "The class '{0}' can't be used as a mixin because it isn't a mixin class nor a mixin."
16021602
comment: |-
16031603
Parameters:
16041604
0: the name of the class being used as a mixin
@@ -12697,15 +12697,17 @@ CompileTimeErrorCode:
1269712697
}
1269812698
```
1269912699
SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY:
12700-
problemMessage: "The sealed class '{0}' can't be extended, implemented, or mixed in outside of its library."
12700+
sharedName: INVALID_USE_OF_TYPE_OUTSIDE_LIBRARY
12701+
problemMessage: "The class '{0}' can't be extended, implemented, or mixed in outside of its library because it's a sealed class."
1270112702
comment: |-
1270212703
Parameters:
12703-
0: the name of the supertype being extended, implemented, or mixed in
12704+
0: the name of the sealed class being extended, implemented, or mixed in
1270412705
SEALED_MIXIN_SUBTYPE_OUTSIDE_OF_LIBRARY:
12705-
problemMessage: "The sealed mixin '{0}' can't be mixed in outside of its library."
12706+
sharedName: INVALID_USE_OF_TYPE_OUTSIDE_LIBRARY
12707+
problemMessage: "The mixin '{0}' can't be mixed in outside of its library because it's a sealed mixin."
1270612708
comment: |-
1270712709
Parameters:
12708-
0: the name of the supertype being mixed in
12710+
0: the name of the sealed mixin being mixed in
1270912711
SET_ELEMENT_TYPE_NOT_ASSIGNABLE:
1271012712
problemMessage: "The element type '{0}' can't be assigned to the set type '{1}'."
1271112713
hasPublishedDocs: true

pkg/analyzer/test/src/diagnostics/class_used_as_mixin_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Foo {}
4646
import 'foo.dart';
4747
class Bar with Foo {}
4848
''', [
49-
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 19, 21),
49+
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 34, 3),
5050
]);
5151
}
5252

@@ -72,7 +72,7 @@ class Foo {}
7272
import 'foo.dart';
7373
class Bar with Foo {}
7474
''', [
75-
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 35, 21),
75+
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 50, 3),
7676
]);
7777
}
7878

@@ -97,7 +97,7 @@ typedef FooTypedef = Foo;
9797
import 'foo.dart';
9898
class Bar with FooTypedef {}
9999
''', [
100-
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 19, 28),
100+
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 34, 10),
101101
]);
102102
}
103103

@@ -136,7 +136,7 @@ import 'foo.dart';
136136
typedef FooTypedef = Foo;
137137
class Bar with FooTypedef {}
138138
''', [
139-
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 45, 28),
139+
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 60, 10),
140140
]);
141141
}
142142

pkg/analyzer/test/src/diagnostics/sealed_class_subtype_outside_of_library_test.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import 'foo.dart';
3232
class Bar extends Foo {}
3333
''', [
3434
error(
35-
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 19, 24),
35+
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 37, 3),
3636
]);
3737
}
3838

@@ -47,7 +47,7 @@ import 'foo.dart';
4747
class Bar extends FooTypedef {}
4848
''', [
4949
error(
50-
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 19, 31),
50+
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 37, 10),
5151
]);
5252
}
5353

@@ -62,7 +62,7 @@ typedef FooTypedef = Foo;
6262
class Bar extends FooTypedef {}
6363
''', [
6464
error(
65-
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 45, 31),
65+
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 63, 10),
6666
]);
6767
}
6868

@@ -95,7 +95,7 @@ import 'foo.dart';
9595
class Bar implements Foo {}
9696
''', [
9797
error(
98-
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 19, 27),
98+
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 40, 3),
9999
]);
100100
}
101101

@@ -110,7 +110,7 @@ import 'foo.dart';
110110
class Bar implements FooTypedef {}
111111
''', [
112112
error(
113-
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 19, 34),
113+
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 40, 10),
114114
]);
115115
}
116116

@@ -125,7 +125,7 @@ typedef FooTypedef = Foo;
125125
class Bar implements FooTypedef {}
126126
''', [
127127
error(
128-
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 45, 34),
128+
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 66, 10),
129129
]);
130130
}
131131

@@ -157,9 +157,9 @@ sealed class Foo {}
157157
import 'foo.dart';
158158
class Bar with Foo {}
159159
''', [
160-
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 19, 21),
160+
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 34, 3),
161161
error(
162-
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 19, 21),
162+
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 34, 3),
163163
]);
164164
}
165165

@@ -173,9 +173,9 @@ typedef FooTypedef = Foo;
173173
import 'foo.dart';
174174
class Bar with FooTypedef {}
175175
''', [
176-
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 19, 28),
176+
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 34, 10),
177177
error(
178-
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 19, 28),
178+
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 34, 10),
179179
]);
180180
}
181181

@@ -189,9 +189,9 @@ import 'foo.dart';
189189
typedef FooTypedef = Foo;
190190
class Bar with FooTypedef {}
191191
''', [
192-
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 45, 28),
192+
error(CompileTimeErrorCode.CLASS_USED_AS_MIXIN, 60, 10),
193193
error(
194-
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 45, 28),
194+
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 60, 10),
195195
]);
196196
}
197197

@@ -217,7 +217,7 @@ import 'foo.dart';
217217
mixin Bar implements Foo {}
218218
''', [
219219
error(
220-
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 19, 27),
220+
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY, 40, 3),
221221
]);
222222
}
223223
}

0 commit comments

Comments
 (0)