Skip to content

Commit a1ee1f6

Browse files
chloestefantsovaCommit Queue
authored and
Commit Queue
committed
[cfe] Implement a desugaring for simple if-case statements
Part of #49749 Change-Id: I38c486d4982c9dbd5515c36eb8c38cc76ff09800 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/265760 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent 98afb38 commit a1ee1f6

File tree

109 files changed

+1567
-137
lines changed

Some content is hidden

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

109 files changed

+1567
-137
lines changed

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

+19-2
Original file line numberDiff line numberDiff line change
@@ -2317,6 +2317,11 @@ class BodyBuilder extends StackListenerImpl
23172317
libraryFeatures.patterns, case_.charOffset, case_.charCount);
23182318
Matcher matcher = toMatcher(pop());
23192319
Expression expression = popForValue();
2320+
for (VariableDeclaration variable in matcher.declaredVariables) {
2321+
// Skip synthetic variables.
2322+
if (variable.name == null) continue;
2323+
declareVariable(variable, scope);
2324+
}
23202325
push(new Condition(expression, matcher, guard));
23212326
} else {
23222327
assert(checkState(token, [
@@ -8331,8 +8336,20 @@ class BodyBuilder extends StackListenerImpl
83318336
libraryFeatures.patterns, variable.charOffset, variable.charCount);
83328337
// ignore: unused_local_variable
83338338
TypeBuilder? type = pop(NullValue.TypeBuilder) as TypeBuilder?;
8334-
// TODO(johnniwinther): Create a variable binder
8335-
push(new DummyBinder());
8339+
DartType? patternType = type?.build(libraryBuilder, TypeUse.variableType);
8340+
Binder binder;
8341+
if (variable.lexeme == "_") {
8342+
binder = new WildcardBinder(patternType, offset: variable.charOffset);
8343+
} else {
8344+
binder = new VariableBinder(
8345+
patternType,
8346+
variable.lexeme,
8347+
forest.createVariableDeclaration(variable.charOffset, variable.lexeme,
8348+
type: patternType,
8349+
isFinal: Modifier.validateVarFinalOrConst(keyword?.lexeme) ==
8350+
finalMask));
8351+
}
8352+
push(binder);
83368353
}
83378354

83388355
@override

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,8 @@ class Forest {
664664
isInitializingFormal: isInitializingFormal,
665665
isCovariantByDeclaration: isCovariantByDeclaration,
666666
isLocalFunction: isLocalFunction,
667-
hasDeclaredInitializer: initializer != null);
667+
hasDeclaredInitializer: initializer != null)
668+
..fileOffset = fileOffset;
668669
}
669670

670671
VariableDeclarationImpl createVariableDeclarationForValue(
@@ -805,6 +806,20 @@ class Forest {
805806
..fileOffset = fileOffset;
806807
}
807808

809+
VariableGet createVariableGet(int fileOffset, VariableDeclaration variable) {
810+
// ignore: unnecessary_null_comparison
811+
assert(fileOffset != null);
812+
return new VariableGetImpl(variable, forNullGuardedAccess: false)
813+
..fileOffset = fileOffset;
814+
}
815+
816+
VariableSet createVariableSet(
817+
int fileOffset, VariableDeclaration variable, Expression value) {
818+
// ignore: unnecessary_null_comparison
819+
assert(fileOffset != null);
820+
return new VariableSet(variable, value)..fileOffset = fileOffset;
821+
}
822+
808823
EqualsExpression createEquals(
809824
int fileOffset, Expression left, Expression right,
810825
{required bool isNot}) {

0 commit comments

Comments
 (0)