Skip to content

Commit 253764e

Browse files
johnniwinthercommit-bot@chromium.org
authored andcommitted
[cfe] Avoid using inferrer.helper.uri in top-level inference
Change-Id: Ifb0bbf579f8728fe14a9330631d4fd90ccb78e58 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/204801 Reviewed-by: Dmitry Stefantsov <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent 83376bf commit 253764e

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

pkg/front_end/lib/src/fasta/kernel/implicit_field_type.dart

-3
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,6 @@ class _ImplicitFieldTypeRoot extends ImplicitFieldType {
153153
bodyBuilder.parseFieldInitializer(initializerToken);
154154
initializerToken = null;
155155

156-
// The type is needed in the inference below, so the helper should be set.
157-
typeInferrer.helper = bodyBuilder;
158-
159156
ExpressionInferenceResult result = typeInferrer.inferExpression(
160157
initializer, const UnknownType(), true,
161158
isVoidAllowed: true);

pkg/front_end/lib/src/fasta/kernel/inference_visitor.dart

+40-6
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,35 @@ class InferenceVisitor
6363

6464
InferenceVisitor(this.inferrer);
6565

66+
/// Computes uri and offset for [node] for internal errors in a way that is
67+
/// safe for both top-level and full inference.
68+
_UriOffset _computeUriOffset(TreeNode node) {
69+
Uri uri;
70+
int fileOffset;
71+
if (!inferrer.isTopLevel) {
72+
// In local inference we have access to the current file uri.
73+
uri = inferrer.helper.uri;
74+
fileOffset = node.fileOffset;
75+
} else {
76+
Location location = node.location;
77+
if (location != null) {
78+
// Use the location file uri, if available.
79+
uri = location.file;
80+
fileOffset = node.fileOffset;
81+
} else {
82+
// Otherwise use the library file uri with no offset.
83+
uri = inferrer.library.fileUri;
84+
fileOffset = TreeNode.noOffset;
85+
}
86+
}
87+
return new _UriOffset(uri, fileOffset);
88+
}
89+
6690
ExpressionInferenceResult _unhandledExpression(
6791
Expression node, DartType typeContext) {
68-
unhandled("${node.runtimeType}", "InferenceVisitor", node.fileOffset,
69-
inferrer.helper.uri);
92+
_UriOffset uriOffset = _computeUriOffset(node);
93+
unhandled("${node.runtimeType}", "InferenceVisitor", uriOffset.fileOffset,
94+
uriOffset.uri);
7095
}
7196

7297
@override
@@ -220,8 +245,9 @@ class InferenceVisitor
220245
}
221246

222247
StatementInferenceResult _unhandledStatement(Statement node) {
223-
return unhandled("${node.runtimeType}", "InferenceVisitor", node.fileOffset,
224-
inferrer.helper.uri);
248+
_UriOffset uriOffset = _computeUriOffset(node);
249+
return unhandled("${node.runtimeType}", "InferenceVisitor",
250+
uriOffset.fileOffset, uriOffset.uri);
225251
}
226252

227253
@override
@@ -991,11 +1017,12 @@ class InferenceVisitor
9911017
} else if (syntheticAssignment is InvalidExpression || hasProblem) {
9921018
return new InvalidForInVariable(syntheticAssignment);
9931019
} else {
1020+
_UriOffset uriOffset = _computeUriOffset(syntheticAssignment);
9941021
return unhandled(
9951022
"${syntheticAssignment.runtimeType}",
9961023
"handleForInStatementWithoutVariable",
997-
syntheticAssignment.fileOffset,
998-
inferrer.helper.uri);
1024+
uriOffset.fileOffset,
1025+
uriOffset.uri);
9991026
}
10001027
}
10011028

@@ -7262,3 +7289,10 @@ class InvalidForInVariable implements ForInVariable {
72627289
Expression inferAssignment(TypeInferrerImpl inferrer, DartType rhsType) =>
72637290
expression;
72647291
}
7292+
7293+
class _UriOffset {
7294+
final Uri uri;
7295+
final int fileOffset;
7296+
7297+
_UriOffset(this.uri, this.fileOffset);
7298+
}

0 commit comments

Comments
 (0)