Skip to content

Commit

Permalink
[analyzer] Use parameter.nameOffset for synthetic arguments for super…
Browse files Browse the repository at this point in the history
… params in constant evaluation

These synthetic nodes might be used in diagnostics/contextMessages so they should map to reasonable offsets.

Fixes Dart-Code/Dart-Code#5359

Change-Id: I65aee5f04ccc074ce943e7043dd8f41d4b6a0847
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/399661
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
DanTup authored and Commit Queue committed Dec 9, 2024
1 parent 46f53d4 commit da13398
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/analyzer/lib/src/dart/constant/evaluation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2593,7 +2593,7 @@ class _InstanceCreationEvaluator {
for (var parameter in _constructor.parameters) {
if (parameter is SuperFormalParameterElement) {
var value = SimpleIdentifierImpl(
StringToken(TokenType.STRING, parameter.name, -1),
StringToken(TokenType.STRING, parameter.name, parameter.nameOffset),
)
..staticElement = parameter
..setPseudoExpressionStaticType(parameter.type);
Expand All @@ -2604,7 +2604,8 @@ class _InstanceCreationEvaluator {
NamedExpressionImpl(
name: LabelImpl(
label: SimpleIdentifierImpl(
StringToken(TokenType.STRING, parameter.name, -1),
StringToken(
TokenType.STRING, parameter.name, parameter.nameOffset),
)..staticElement = parameter,
colon: StringToken(TokenType.COLON, ':', -1),
),
Expand Down
114 changes: 114 additions & 0 deletions pkg/analyzer/test/src/dart/constant/evaluation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2076,6 +2076,120 @@ const c = _;
''');
}

test_visitInstanceCreationExpression_invalidNamedArg() async {
await assertErrorsInCode('''
class A {
const A({ required int x });
}
const a = A(x: false);
''', [
error(CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE, 58, 5),
error(CompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, 55, 8),
]);
}

test_visitInstanceCreationExpression_invalidNamedArg_superParam() async {
await assertErrorsInCode('''
class A {
const A({ required int x });
}
class B extends A {
const B({ required super.x });
}
const a = B(x: false);
''', [
error(CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE, 113, 5),
error(CompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, 110, 8),
]);
}

test_visitInstanceCreationExpression_invalidPositionalArg() async {
await assertErrorsInCode('''
class A {
const A(int x);
}
const a = A(false);
''', [
error(CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE, 42, 5),
error(CompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, 42, 5),
]);
}

test_visitInstanceCreationExpression_invalidPositionalArg_superParam() async {
await assertErrorsInCode('''
class A {
const A(int x);
}
class B extends A {
const B(super.x);
}
const a = B(false);
''', [
error(CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE, 84, 5),
error(CompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, 84, 5),
]);
}

test_visitInstanceCreationExpression_missingNamedArg() async {
await assertErrorsInCode('''
class A {
const A({required int x });
}
const a = A();
''', [
error(CompileTimeErrorCode.MISSING_REQUIRED_ARGUMENT, 52, 1),
]);
}

test_visitInstanceCreationExpression_missingNamedArg_superParam() async {
await assertErrorsInCode('''
class A {
const A({required int x });
}
class B extends A {
const B({required super.x });
}
const a = B();
''', [
error(CompileTimeErrorCode.MISSING_REQUIRED_ARGUMENT, 106, 1),
error(CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE, 106,
3),
error(CompileTimeErrorCode.INVALID_CONSTANT, 106, 3,
contextMessages: [message(testFile, 88, 1)]),
]);
}

test_visitInstanceCreationExpression_missingPositionalArg() async {
await assertErrorsInCode('''
class A {
const A(int x);
}
const a = A();
''', [
error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR,
42, 1),
]);
}

test_visitInstanceCreationExpression_missingPositionalArg_superParam() async {
await assertErrorsInCode('''
class A {
const A(int x);
}
class B extends A {
const B(super.x);
}
const a = B();
''', [
error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR,
84, 1),
error(CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE, 82,
3),
error(CompileTimeErrorCode.INVALID_CONSTANT, 82, 3,
contextMessages: [message(testFile, 66, 1)]),
]);
}

test_visitInstanceCreationExpression_noArgs() async {
await assertNoErrorsInCode('''
class A {
Expand Down

0 comments on commit da13398

Please sign in to comment.