Skip to content

Commit 2b492a8

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Add UriOffsetLength
This adds a UriOffsetLength that passes uri, offset and length of a source locations. This is used to support field/getter/setterUriOffset to prepare for getter/setter and final field/setter pairs to be contained in the same builder object, while still allowing messaging to point to the specific aspect. UriOffsetLength is furthermore used on ClassMember which improves the precision of messages from hierarchy checks. Change-Id: I0776c8b66177164e326d3fb61e32a7620955f4c7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/429961 Reviewed-by: Jens Johansen <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent b00c777 commit 2b492a8

File tree

51 files changed

+608
-353
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+608
-353
lines changed

pkg/front_end/lib/src/base/crash.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,6 @@ Future<T> withCrashReporting<T>(
146146
rethrow;
147147
}
148148
UriOffset? uriOffset = currentUriOffset();
149-
return reportCrash(e, s, uriOffset?.uri, uriOffset?.fileOffset);
149+
return reportCrash(e, s, uriOffset?.fileUri, uriOffset?.fileOffset);
150150
}
151151
}

pkg/front_end/lib/src/base/scope.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,10 @@ NamedBuilder computeAmbiguousDeclarationForImport(
371371
// We report the error lazily (setting suppressMessage to false) because the
372372
// spec 18.1 states that 'It is not an error if N is introduced by two or
373373
// more imports but never referred to.'
374-
return new InvalidTypeDeclarationBuilder(name,
375-
message.withLocation(uriOffset.uri, uriOffset.fileOffset, name.length),
374+
return new InvalidTypeDeclarationBuilder(
375+
name,
376+
message.withLocation(
377+
uriOffset.fileUri, uriOffset.fileOffset, name.length),
376378
suppressMessage: false);
377379
}
378380

pkg/front_end/lib/src/base/uri_offset.dart

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,62 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
class UriOffset {
6-
final Uri uri;
5+
import 'messages.dart';
6+
7+
/// Source location pointer used for messaging.
8+
class UriOffset implements UriOffsetLength {
9+
/// The file URI of the location.
10+
@override
11+
final Uri fileUri;
12+
13+
/// The character offset for the location with [fileUri].
14+
@override
715
final int fileOffset;
816

9-
UriOffset(this.uri, this.fileOffset);
17+
UriOffset(this.fileUri, this.fileOffset);
18+
19+
@override
20+
int get length => noLength;
21+
}
22+
23+
/// Source location pointer used for messaging.
24+
class UriOffsetLength {
25+
/// The file URI of the location.
26+
final Uri fileUri;
27+
28+
/// The character offset for the location with [fileUri].
29+
final int fileOffset;
30+
31+
/// The length of the location.
32+
///
33+
/// This is used to emitted the correct number of `^` characters in the
34+
/// message output for the source location.
35+
final int length;
36+
37+
UriOffsetLength(this.fileUri, this.fileOffset, this.length);
38+
}
39+
40+
extension ProblemReportingExtension on ProblemReporting {
41+
/// Helper method for calling [ProblemReporting.addProblem] with a
42+
/// [UriOffsetLength].
43+
void addProblem2(Message message, UriOffsetLength uriOffset,
44+
{bool wasHandled = false,
45+
List<LocatedMessage>? context,
46+
Severity? severity,
47+
bool problemOnLibrary = false}) {
48+
addProblem(
49+
message, uriOffset.fileOffset, uriOffset.length, uriOffset.fileUri,
50+
wasHandled: wasHandled,
51+
context: context,
52+
severity: severity,
53+
problemOnLibrary: problemOnLibrary);
54+
}
55+
}
56+
57+
extension MessageExtension on Message {
58+
/// Helper method for calling [Message.withLocation] with a [UriOffsetLength].
59+
LocatedMessage withLocation2(UriOffsetLength uriOffset) {
60+
return withLocation(
61+
uriOffset.fileUri, uriOffset.fileOffset, uriOffset.length);
62+
}
1063
}

pkg/front_end/lib/src/builder/declaration_builder.dart

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ abstract class IDeclarationBuilder implements ITypeDeclarationBuilder {
2424
MemberBuilder? findConstructorOrFactory(
2525
String name, int charOffset, Uri uri, LibraryBuilder accessingLibrary);
2626

27-
void addProblem(Message message, int charOffset, int length,
28-
{bool wasHandled = false, List<LocatedMessage>? context});
29-
3027
/// Returns the type of `this` in an instance of this declaration.
3128
///
3229
/// This is non-null for class and mixin declarations and `null` for
@@ -92,11 +89,4 @@ abstract class DeclarationBuilderImpl extends TypeDeclarationBuilderImpl
9289

9390
return declaration;
9491
}
95-
96-
@override
97-
void addProblem(Message message, int charOffset, int length,
98-
{bool wasHandled = false, List<LocatedMessage>? context}) {
99-
libraryBuilder.addProblem(message, charOffset, length, fileUri,
100-
wasHandled: wasHandled, context: context);
101-
}
10292
}

pkg/front_end/lib/src/builder/member_builder.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,10 @@ abstract class MemberBuilderImpl extends NamedBuilderImpl
167167
abstract class BuilderClassMember implements ClassMember {
168168
MemberBuilderImpl get memberBuilder;
169169

170-
@override
171-
int get charOffset => memberBuilder.fileOffset;
172-
173170
@override
174171
DeclarationBuilder get declarationBuilder =>
175172
memberBuilder.declarationBuilder!;
176173

177-
@override
178-
Uri get fileUri => memberBuilder.fileUri;
179-
180174
@override
181175
// Coverage-ignore(suite): Not run.
182176
bool get isExtensionTypeMember => memberBuilder.isExtensionTypeMember;

pkg/front_end/lib/src/builder/property_builder.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import '../base/uri_offset.dart';
56
import 'builder.dart';
67
import 'member_builder.dart';
78

@@ -23,6 +24,14 @@ abstract class PropertyBuilder implements MemberBuilder {
2324
/// Returns a [SetterQuality] enum value that describes what kind of setter
2425
/// this property has, if any.
2526
SetterQuality get setterQuality;
27+
28+
/// Returns the [UriOffsetLength] for the introductory declaration of the
29+
/// getter aspect, if any.
30+
UriOffsetLength? get getterUriOffset;
31+
32+
/// Returns the [UriOffsetLength] for the introductory declaration of the
33+
/// setter aspect, if any.
34+
UriOffsetLength? get setterUriOffset;
2635
}
2736

2837
/// Enum for the different ways a property can have a field aspect.

pkg/front_end/lib/src/dill/dill_builder_mixins.dart

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:kernel/ast.dart';
66
import 'package:kernel/class_hierarchy.dart';
77

8+
import '../base/uri_offset.dart';
89
import '../builder/builder.dart';
910
import '../builder/constructor_builder.dart';
1011
import '../builder/declaration_builders.dart';
@@ -97,16 +98,16 @@ mixin DillFieldBuilderMixin implements DillMemberBuilder, PropertyBuilder {
9798
List<ClassMember>? _localSetters;
9899

99100
@override
100-
List<ClassMember> get localMembers =>
101-
_localMembers ??= !member.isInternalImplementation
102-
? [new DillClassMember(this, ClassMemberKind.Getter)]
103-
: const [];
101+
List<ClassMember> get localMembers => _localMembers ??= !member
102+
.isInternalImplementation
103+
? [new DillClassMember(this, ClassMemberKind.Getter, getterUriOffset!)]
104+
: const [];
104105

105106
@override
106-
List<ClassMember> get localSetters =>
107-
_localSetters ??= hasSetter && !member.isInternalImplementation
108-
? [new DillClassMember(this, ClassMemberKind.Setter)]
109-
: const [];
107+
List<ClassMember> get localSetters => _localSetters ??= hasSetter &&
108+
!member.isInternalImplementation
109+
? [new DillClassMember(this, ClassMemberKind.Setter, setterUriOffset!)]
110+
: const [];
110111
}
111112

112113
mixin DillGetterBuilderMixin implements DillMemberBuilder, PropertyBuilder {
@@ -135,13 +136,17 @@ mixin DillGetterBuilderMixin implements DillMemberBuilder, PropertyBuilder {
135136
@override
136137
NamedBuilder? get setable => null;
137138

139+
@override
140+
// Coverage-ignore(suite): Not run.
141+
UriOffsetLength? get setterUriOffset => null;
142+
138143
List<ClassMember>? _localMembers;
139144

140145
@override
141-
List<ClassMember> get localMembers =>
142-
_localMembers ??= !member.isInternalImplementation
143-
? [new DillClassMember(this, ClassMemberKind.Getter)]
144-
: const [];
146+
List<ClassMember> get localMembers => _localMembers ??= !member
147+
.isInternalImplementation
148+
? [new DillClassMember(this, ClassMemberKind.Getter, getterUriOffset!)]
149+
: const [];
145150

146151
@override
147152
List<ClassMember> get localSetters => const [];
@@ -177,16 +182,20 @@ mixin DillSetterBuilderMixin implements DillMemberBuilder, PropertyBuilder {
177182
@override
178183
NamedBuilder get setable => this;
179184

185+
@override
186+
// Coverage-ignore(suite): Not run.
187+
UriOffsetLength? get getterUriOffset => null;
188+
180189
List<ClassMember>? _localSetters;
181190

182191
@override
183192
List<ClassMember> get localMembers => const [];
184193

185194
@override
186-
List<ClassMember> get localSetters =>
187-
_localSetters ??= !member.isInternalImplementation
188-
? [new DillClassMember(this, ClassMemberKind.Setter)]
189-
: const [];
195+
List<ClassMember> get localSetters => _localSetters ??= !member
196+
.isInternalImplementation
197+
? [new DillClassMember(this, ClassMemberKind.Setter, setterUriOffset!)]
198+
: const [];
190199
}
191200

192201
mixin DillMethodBuilderMixin implements DillMemberBuilder, MethodBuilder {
@@ -212,10 +221,12 @@ mixin DillMethodBuilderMixin implements DillMemberBuilder, MethodBuilder {
212221

213222
List<ClassMember>? _localMembers;
214223

224+
UriOffsetLength get uriOffset;
225+
215226
@override
216227
List<ClassMember> get localMembers =>
217228
_localMembers ??= !member.isInternalImplementation
218-
? [new DillClassMember(this, ClassMemberKind.Method)]
229+
? [new DillClassMember(this, ClassMemberKind.Method, uriOffset)]
219230
: const [];
220231

221232
@override
@@ -252,10 +263,12 @@ mixin DillOperatorBuilderMixin implements DillMemberBuilder, MethodBuilder {
252263

253264
List<ClassMember>? _localMembers;
254265

266+
UriOffsetLength get uriOffset;
267+
255268
@override
256269
List<ClassMember> get localMembers =>
257270
_localMembers ??= !member.isInternalImplementation
258-
? [new DillClassMember(this, ClassMemberKind.Method)]
271+
? [new DillClassMember(this, ClassMemberKind.Method, uriOffset)]
259272
: const [];
260273

261274
@override

pkg/front_end/lib/src/dill/dill_extension_member_builder.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:kernel/ast.dart';
6+
import 'package:kernel/names.dart';
67

8+
import '../base/uri_offset.dart';
79
import '../builder/method_builder.dart';
810
import '../builder/property_builder.dart';
911
import 'dill_builder_mixins.dart';
@@ -74,6 +76,17 @@ class DillExtensionFieldBuilder extends DillExtensionMemberBuilder
7476
// Coverage-ignore(suite): Not run.
7577
GetterQuality get getterQuality => GetterQuality.Implicit;
7678

79+
@override
80+
// Coverage-ignore(suite): Not run.
81+
UriOffsetLength get getterUriOffset =>
82+
new UriOffsetLength(fileUri, fileOffset, _descriptor.name.text.length);
83+
84+
@override
85+
// Coverage-ignore(suite): Not run.
86+
UriOffsetLength? get setterUriOffset => hasSetter
87+
? new UriOffsetLength(fileUri, fileOffset, _descriptor.name.text.length)
88+
: null;
89+
7790
@override
7891
// Coverage-ignore(suite): Not run.
7992
bool get hasConstField => _field.isConst;
@@ -117,6 +130,11 @@ class DillExtensionSetterBuilder extends DillExtensionMemberBuilder
117130
@override
118131
SetterQuality get setterQuality =>
119132
procedure.isExternal ? SetterQuality.External : SetterQuality.Concrete;
133+
134+
@override
135+
// Coverage-ignore(suite): Not run.
136+
UriOffsetLength get setterUriOffset =>
137+
new UriOffsetLength(fileUri, fileOffset, _descriptor.name.text.length);
120138
}
121139

122140
class DillExtensionGetterBuilder extends DillExtensionMemberBuilder
@@ -160,6 +178,11 @@ class DillExtensionGetterBuilder extends DillExtensionMemberBuilder
160178

161179
@override
162180
SetterQuality get setterQuality => SetterQuality.Absent;
181+
182+
@override
183+
// Coverage-ignore(suite): Not run.
184+
UriOffsetLength get getterUriOffset =>
185+
new UriOffsetLength(fileUri, fileOffset, _descriptor.name.text.length);
163186
}
164187

165188
class DillExtensionOperatorBuilder extends DillExtensionMemberBuilder
@@ -189,6 +212,11 @@ class DillExtensionOperatorBuilder extends DillExtensionMemberBuilder
189212
@override
190213
// Coverage-ignore(suite): Not run.
191214
Iterable<Reference> get exportedMemberReferences => [_procedure.reference];
215+
216+
@override
217+
// Coverage-ignore(suite): Not run.
218+
UriOffsetLength get uriOffset => new UriOffsetLength(fileUri, fileOffset,
219+
_descriptor.name == unaryMinusName ? 1 : _descriptor.name.text.length);
192220
}
193221

194222
class DillExtensionStaticMethodBuilder extends DillExtensionMemberBuilder
@@ -226,6 +254,11 @@ class DillExtensionStaticMethodBuilder extends DillExtensionMemberBuilder
226254
@override
227255
// Coverage-ignore(suite): Not run.
228256
Iterable<Reference> get exportedMemberReferences => [_procedure.reference];
257+
258+
@override
259+
// Coverage-ignore(suite): Not run.
260+
UriOffsetLength get uriOffset =>
261+
new UriOffsetLength(fileUri, fileOffset, _descriptor.name.text.length);
229262
}
230263

231264
class DillExtensionInstanceMethodBuilder extends DillExtensionMemberBuilder
@@ -266,4 +299,9 @@ class DillExtensionInstanceMethodBuilder extends DillExtensionMemberBuilder
266299
@override
267300
// Coverage-ignore(suite): Not run.
268301
Reference get invokeTargetReference => _procedure.reference;
302+
303+
@override
304+
// Coverage-ignore(suite): Not run.
305+
UriOffsetLength get uriOffset =>
306+
new UriOffsetLength(fileUri, fileOffset, _descriptor.name.text.length);
269307
}

0 commit comments

Comments
 (0)