@@ -252,6 +252,9 @@ class BodyBuilder extends StackListenerImpl
252
252
return _defaultValueNestingLevel > 0 ;
253
253
}
254
254
255
+ /// True if the parser is between [beginMetadata] and [endMetadata] .
256
+ bool _insideMetadataParsing = false ;
257
+
255
258
/// Numeric nestedness of formal parameter default values.
256
259
///
257
260
/// The value of 0 means that the currently built part is not within a default
@@ -292,10 +295,36 @@ class BodyBuilder extends StackListenerImpl
292
295
/// values or inside the default values that aren't built as outline
293
296
/// expressions need to be added during the second pass.
294
297
bool get _createdStaticInvocationsNeedPostProcessing {
295
- return _context.hasFormalParameters &&
296
- ! _insideOfFormalParameterDefaultValue ||
297
- ! _context.hasImmediateOutlineExpressionsBuilt ||
298
- ! _context.needsImmediateValuesBuiltAsOutlineExpressions;
298
+ return
299
+ // All invocations in outline building phase will be type-inferred, and
300
+ // they all should be added to the post-processing.
301
+ _context.inOutlineBuildingPhase ||
302
+
303
+ // Here we aren't in the outline mode, but rather in the
304
+ // body-building mode. If the current context has formal parameters,
305
+ // their default values should be skipped because in the
306
+ // body-building mode they aren't passed through type inference. An
307
+ // exception here is the default values of instance methods: they
308
+ // are actually inferred in body-building phase, and they aren't
309
+ // built at all during the outline phase.
310
+ (! _context.hasFormalParameters ||
311
+ ! _insideOfFormalParameterDefaultValue ||
312
+ ! isDeclarationInstanceContext) &&
313
+
314
+ // The invocations in the metadata should also be skipped in the
315
+ // body-building phase, since they aren't type-inferred. An
316
+ // exception here are the annotations within method bodies,
317
+ // field initializers, and on formal parameters.
318
+ ! (_context.inMetadata ||
319
+ _insideMetadataParsing &&
320
+ ! _inBody &&
321
+ ! inFormals &&
322
+ ! inFieldInitializer) &&
323
+
324
+ // Finally, the const fields in body-building phase aren't
325
+ // inferred and the invocations in them should be skipped during
326
+ // post-processing.
327
+ ! _context.inConstFields;
299
328
}
300
329
301
330
bool get inFunctionType =>
@@ -315,6 +344,10 @@ class BodyBuilder extends StackListenerImpl
315
344
316
345
int functionNestingLevel = 0 ;
317
346
347
+ int _inBodyCount = 0 ;
348
+
349
+ bool get _inBody => _inBodyCount > 0 ;
350
+
318
351
Statement ? problemInLoopOrSwitch;
319
352
320
353
Scope ? switchScope;
@@ -475,6 +508,9 @@ class BodyBuilder extends StackListenerImpl
475
508
void enterLocalScope (Scope localScope) {
476
509
push (scope);
477
510
scope = localScope;
511
+ if (scope.kind == ScopeKind .functionBody) {
512
+ _inBodyCount++ ;
513
+ }
478
514
assert (checkState (null , [
479
515
ValueKinds .Scope ,
480
516
]));
@@ -484,6 +520,9 @@ class BodyBuilder extends StackListenerImpl
484
520
{required String debugName, required ScopeKind kind}) {
485
521
push (scope);
486
522
scope = scope.createNestedScope (debugName: debugName, kind: kind);
523
+ if (kind == ScopeKind .functionBody) {
524
+ _inBodyCount++ ;
525
+ }
487
526
assert (checkState (null , [
488
527
ValueKinds .Scope ,
489
528
]));
@@ -508,6 +547,9 @@ class BodyBuilder extends StackListenerImpl
508
547
declaredInCurrentGuard = null ;
509
548
}
510
549
}
550
+ if (scope.kind == ScopeKind .functionBody) {
551
+ _inBodyCount-- ;
552
+ }
511
553
scope = pop () as Scope ;
512
554
}
513
555
@@ -867,6 +909,7 @@ class BodyBuilder extends StackListenerImpl
867
909
super .push (constantContext);
868
910
constantContext = ConstantContext .inferred;
869
911
assert (checkState (token, [ValueKinds .ConstantContext ]));
912
+ _insideMetadataParsing = true ;
870
913
}
871
914
872
915
@override
@@ -941,6 +984,7 @@ class BodyBuilder extends StackListenerImpl
941
984
}
942
985
constantContext = savedConstantContext;
943
986
}
987
+ _insideMetadataParsing = false ;
944
988
assert (checkState (beginToken, [ValueKinds .Expression ]));
945
989
}
946
990
0 commit comments