Skip to content

Commit

Permalink
Ensure that lists and maps built by ExprBuilder have their staticType…
Browse files Browse the repository at this point in the history
… set.

We can't rely on resolution to do this, because resolution only
operates in the compilation unit currently being analyzed, and
constant values may be needed from other compilation units.

Fixes dart-lang#35230.

Change-Id: I11fa5597110bc13164ca5dc9846836848120cd08
Reviewed-on: https://dart-review.googlesource.com/c/84964
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
  • Loading branch information
stereotype441 authored and commit-bot@chromium.org committed Nov 20, 2018
1 parent 9ca6369 commit c165ef8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pkg/analyzer/lib/src/summary/expr_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,12 @@ class ExprBuilder {
for (int i = 0; i < count; i++) {
elements.insert(0, _pop());
}
_push(AstTestFactory.listLiteral2(Keyword.CONST, typeArguments, elements));
var typeArg = typeArguments == null
? resynthesizer.typeProvider.dynamicType
: typeArguments.arguments[0].type;
var staticType = resynthesizer.typeProvider.listType.instantiate([typeArg]);
_push(AstTestFactory.listLiteral2(Keyword.CONST, typeArguments, elements)
..staticType = staticType);
}

void _pushLocalFunctionReference() {
Expand Down Expand Up @@ -770,7 +775,16 @@ class ExprBuilder {
Expression key = _pop();
entries.insert(0, AstTestFactory.mapLiteralEntry2(key, value));
}
_push(AstTestFactory.mapLiteral(Keyword.CONST, typeArguments, entries));
var keyType = typeArguments == null
? resynthesizer.typeProvider.dynamicType
: typeArguments.arguments[0].type;
var valueType = typeArguments == null
? resynthesizer.typeProvider.dynamicType
: typeArguments.arguments[1].type;
var staticType =
resynthesizer.typeProvider.mapType.instantiate([keyType, valueType]);
_push(AstTestFactory.mapLiteral(Keyword.CONST, typeArguments, entries)
..staticType = staticType);
}

void _pushPrefix(TokenType operator) {
Expand Down
46 changes: 46 additions & 0 deletions pkg/analyzer/test/generated/non_error_resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3591,6 +3591,52 @@ void main() {
verify([source]);
}

test_issue_35320_lists() async {
addNamedSource('/lib.dart', '''
const x = const <String>['a'];
''');
Source source = addSource('''
import 'lib.dart';
const y = const <String>['b'];
int f(v) {
switch(v) {
case x:
return 0;
case y:
return 1;
default:
return 2;
}
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}

test_issue_35320_maps() async {
addNamedSource('/lib.dart', '''
const x = const <String, String>{'a': 'b'};
''');
Source source = addSource('''
import 'lib.dart';
const y = const <String, String>{'c': 'd'};
int f(v) {
switch(v) {
case x:
return 0;
case y:
return 1;
default:
return 2;
}
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}

test_listElementTypeNotAssignable() async {
Source source = addSource(r'''
var v1 = <int> [42];
Expand Down

0 comments on commit c165ef8

Please sign in to comment.