Skip to content

Commit e25adcf

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Check name conflicts using fragments
Change-Id: I7ffe54bdce190de5b31303fd794947aba3d3b1c3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/393102 Commit-Queue: Johnni Winther <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent 5e6a681 commit e25adcf

19 files changed

+378
-134
lines changed

pkg/front_end/lib/src/fragment/extension.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ExtensionFragment extends DeclarationFragment implements Fragment {
3636
? new FixedExtensionName(name)
3737
: new UnnamedExtensionName();
3838

39+
bool get isUnnamed => extensionName.isUnnamedExtension;
40+
3941
@override
4042
SourceExtensionBuilder get builder {
4143
assert(_builder != null, "Builder has not been computed for $this.");

pkg/front_end/lib/src/fragment/field.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ class FieldFragment implements Fragment {
3434
: _initializerToken = initializerToken,
3535
_constInitializerToken = constInitializerToken;
3636

37+
// Coverage-ignore(suite): Not run.
38+
bool get hasSetter {
39+
if (modifiers.isFinal) {
40+
if (modifiers.isLate) {
41+
return !modifiers.hasInitializer;
42+
} else {
43+
return false;
44+
}
45+
} else {
46+
return true;
47+
}
48+
}
49+
3750
Token? get initializerToken {
3851
Token? result = _initializerToken;
3952
// Ensure that we don't hold onto the token.

pkg/front_end/lib/src/fragment/util.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,23 @@ class ConstructorName {
1515
final int? nameOffset;
1616

1717
/// The name of the constructor including the enclosing declaration name.
18+
///
19+
/// For unnamed constructors the full name is normalized to be the class name,
20+
/// regardless of whether the constructor was declared with 'new'.
21+
///
22+
/// For invalid constructor names, the full name is normalized to use the
23+
/// class name as prefix, regardless of whether the declaration did so.
24+
///
25+
/// This means that not in all cases is the text pointed to by
26+
/// [fullNameOffset] and [fullNameLength] the same as the [fullName].
1827
final String fullName;
1928

20-
/// The offset at which [fullName] occurs.
29+
/// The offset at which the full name occurs.
2130
///
2231
/// This is used in messages to put the `^` at the start of the [fullName].
2332
final int fullNameOffset;
2433

25-
/// The number of characters of [fullName] that occurs at [fullNameOffset].
34+
/// The number of characters of full name that occurs at [fullNameOffset].
2635
///
2736
/// This is used in messages to put the right amount of `^` under the name.
2837
final int fullNameLength;
@@ -32,5 +41,6 @@ class ConstructorName {
3241
required this.nameOffset,
3342
required this.fullName,
3443
required this.fullNameOffset,
35-
required this.fullNameLength});
44+
required this.fullNameLength})
45+
: assert(name != 'new');
3646
}

pkg/front_end/lib/src/source/source_builder_factory.dart

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,13 +1871,20 @@ class BuilderFactoryImpl implements BuilderFactory, BuilderFactoryResult {
18711871
suffix = identifier.name;
18721872
suffixOffset = identifier.nameOffset;
18731873
charOffset = qualifier.nameOffset;
1874-
fullName = '${prefix}.${suffix}';
1874+
String prefixAndSuffix = '${prefix}.${suffix}';
18751875
fullNameOffset = qualifier.nameOffset;
18761876
// If the there is no space between the prefix and suffix we use the full
18771877
// length as the name length. Otherwise the full name has no length.
18781878
fullNameLength = fullNameOffset + prefix.length + 1 == suffixOffset
1879-
? fullName.length
1879+
? prefixAndSuffix.length
18801880
: noLength;
1881+
if (suffix == "new") {
1882+
// Normalize `Class.new` to `Class`.
1883+
suffix = '';
1884+
fullName = className;
1885+
} else {
1886+
fullName = '$className.$suffix';
1887+
}
18811888
} else {
18821889
prefix = identifier.name;
18831890
suffix = null;
@@ -1887,16 +1894,17 @@ class BuilderFactoryImpl implements BuilderFactory, BuilderFactoryResult {
18871894
fullNameOffset = identifier.nameOffset;
18881895
fullNameLength = prefix.length;
18891896
}
1890-
if (libraryFeatures.constructorTearoffs.isEnabled) {
1891-
suffix = suffix == "new" ? "" : suffix;
1892-
}
1897+
18931898
if (prefix == className) {
18941899
return new ConstructorName(
18951900
name: suffix ?? '',
18961901
nameOffset: suffixOffset,
18971902
fullName: fullName,
18981903
fullNameOffset: fullNameOffset,
18991904
fullNameLength: fullNameLength);
1905+
} else if (suffix == null) {
1906+
// Normalize `foo` in `Class` to `Class.foo`.
1907+
fullName = '$className.$prefix';
19001908
}
19011909
if (suffix == null && !isFactory) {
19021910
// This method is called because the syntax indicated that this is a
@@ -1921,9 +1929,9 @@ class BuilderFactoryImpl implements BuilderFactory, BuilderFactoryResult {
19211929
return new ConstructorName(
19221930
name: suffix ?? prefix,
19231931
nameOffset: suffixOffset,
1924-
fullName: suffix ?? prefix,
1932+
fullName: fullName,
19251933
fullNameOffset: fullNameOffset,
1926-
fullNameLength: noLength);
1934+
fullNameLength: fullNameLength);
19271935
}
19281936

19291937
void _addNativeGetterFragment(GetterFragment fragment) {

pkg/front_end/lib/src/source/source_field_builder.dart

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,8 @@ class SourceFieldBuilder extends SourceMemberBuilderImpl
154154
assert(lateIsSetSetterReference == null);
155155
assert(lateGetterReference == null);
156156
assert(lateSetterReference == null);
157-
_fieldEncoding = new RepresentationFieldEncoding(
158-
this,
159-
name,
160-
nameScheme,
161-
fileUri,
162-
nameOffset,
163-
endOffset,
164-
fieldGetterReference);
157+
_fieldEncoding = new RepresentationFieldEncoding(this, name, nameScheme,
158+
fileUri, nameOffset, endOffset, fieldGetterReference);
165159
} else if (isLate &&
166160
libraryBuilder.loader.target.backendTarget.isLateFieldLoweringEnabled(
167161
hasInitializer: hasInitializer,

0 commit comments

Comments
 (0)