Skip to content

Commit

Permalink
Ensure that prefix?.loadLibrary() generates the proper compile-time e…
Browse files Browse the repository at this point in the history
…rror.

loadLibrary() is special-cased in analyzer, so we need special-case
code to make sure that it can't be invoked using "?.".

Fixes #23463.

R=scheglov@google.com

Review URL: https://codereview.chromium.org//1177833002.
  • Loading branch information
stereotype441 committed Jun 10, 2015
1 parent f2b59c3 commit c03b8fe
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/analyzer/lib/src/generated/element_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,11 @@ class ElementResolver extends SimpleAstVisitor<Object> {
propagatedElement = null;
} else if (methodName.name == FunctionElement.LOAD_LIBRARY_NAME &&
_isDeferredPrefix(target)) {
if (node.operator.type == sc.TokenType.QUESTION_PERIOD) {
_resolver.reportErrorForNode(
CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, target,
[(target as SimpleIdentifier).name]);
}
LibraryElement importedLibrary = _getImportedLibrary(target);
methodName.staticElement = importedLibrary.loadLibraryFunction;
return null;
Expand Down
97 changes: 97 additions & 0 deletions pkg/analyzer/test/generated/compile_time_error_code_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4723,6 +4723,103 @@ f() {
verify([source]);
}

void test_prefix_conditionalPropertyAccess_call_loadLibrary() {
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
options.enableNullAwareOperators = true;
resetWithOptions(options);
addNamedSource('/lib.dart', '''
library lib;
''');
Source source = addSource('''
import 'lib.dart' deferred as p;
f() {
p?.loadLibrary();
}
''');
resolve(source);
assertErrors(
source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
verify([source]);
}

void test_prefix_conditionalPropertyAccess_get() {
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
options.enableNullAwareOperators = true;
resetWithOptions(options);
addNamedSource('/lib.dart', '''
library lib;
var x;
''');
Source source = addSource('''
import 'lib.dart' as p;
f() {
return p?.x;
}
''');
resolve(source);
assertErrors(
source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
verify([source]);
}

void test_prefix_conditionalPropertyAccess_get_loadLibrary() {
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
options.enableNullAwareOperators = true;
resetWithOptions(options);
addNamedSource('/lib.dart', '''
library lib;
''');
Source source = addSource('''
import 'lib.dart' deferred as p;
f() {
return p?.loadLibrary;
}
''');
resolve(source);
assertErrors(
source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
verify([source]);
}

void test_prefix_conditionalPropertyAccess_set() {
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
options.enableNullAwareOperators = true;
resetWithOptions(options);
addNamedSource('/lib.dart', '''
library lib;
var x;
''');
Source source = addSource('''
import 'lib.dart' as p;
f() {
p?.x = null;
}
''');
resolve(source);
assertErrors(
source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
verify([source]);
}

void test_prefix_conditionalPropertyAccess_set_loadLibrary() {
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
options.enableNullAwareOperators = true;
resetWithOptions(options);
addNamedSource('/lib.dart', '''
library lib;
''');
Source source = addSource('''
import 'lib.dart' deferred as p;
f() {
p?.loadLibrary = null;
}
''');
resolve(source);
assertErrors(
source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
verify([source]);
}

void test_prefixCollidesWithTopLevelMembers_functionTypeAlias() {
addNamedSource("/lib.dart", r'''
library lib;
Expand Down

0 comments on commit c03b8fe

Please sign in to comment.