Disallow references to this in field initializers.

Fixes https://github.com/dart-lang/sdk/issues/33858

Change-Id: I619de64353ac5131d949d8e3898617435e9f6dcc
Reviewed-on: https://dart-review.googlesource.com/68920
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
This commit is contained in:
Aske Simon Christensen 2018-08-10 11:12:58 +00:00 committed by commit-bot@chromium.org
parent 5c7d257978
commit f2766eb24e
14 changed files with 98 additions and 55 deletions

View file

@ -42,13 +42,6 @@ class AnalysisDriverResolutionTest_Kernel extends AnalysisDriverResolutionTest {
await super.test_invalid_constructor_initializer_field_importPrefix();
}
@override
@failingTest
@FastaProblem('https://github.com/dart-lang/sdk/issues/33858')
test_invalid_fieldInitializer_this() async {
await super.test_invalid_fieldInitializer_this();
}
@override
@failingTest
test_methodInvocation_topLevelFunction_generic() async {

View file

@ -193,6 +193,8 @@ abstract class BodyBuilder extends ScopeListener<JumpTarget>
/// second in the formal parameter scope.
bool inInitializer = false;
bool inFieldInitializer = false;
bool inCatchClause = false;
bool inCatchBlock = false;
@ -627,11 +629,13 @@ abstract class BodyBuilder extends ScopeListener<JumpTarget>
void beginInitializer(Token token) {
debugEvent("beginInitializer");
inInitializer = true;
inFieldInitializer = true;
}
@override
void endInitializer(Token token) {
debugEvent("endInitializer");
inFieldInitializer = false;
assert(!inInitializer);
final member = this.member;
Object node = pop();
@ -1519,7 +1523,10 @@ abstract class BodyBuilder extends ScopeListener<JumpTarget>
declaration =
classBuilder.origin.findStaticBuilder(name, charOffset, uri, library);
}
if (declaration != null && member.isField && declaration.isInstanceMember) {
if (declaration != null &&
declaration.isInstanceMember &&
inFieldInitializer &&
!inInitializer) {
return new IncompleteErrorGenerator(this, token, declaration.target,
fasta.templateThisAccessInFieldInitializer.withArguments(name));
}
@ -1847,9 +1854,15 @@ abstract class BodyBuilder extends ScopeListener<JumpTarget>
..fileEqualsOffset = offsetForToken(equalsToken));
}
@override
void beginFieldInitializer(Token token) {
inFieldInitializer = true;
}
@override
void endFieldInitializer(Token assignmentOperator, Token token) {
debugEvent("FieldInitializer");
inFieldInitializer = false;
assert(assignmentOperator.stringValue == "=");
push(popForValue());
}
@ -3129,7 +3142,8 @@ abstract class BodyBuilder extends ScopeListener<JumpTarget>
void handleThisExpression(Token token, IdentifierContext context) {
debugEvent("ThisExpression");
if (context.isScopeReference && isInstanceContext) {
push(new ThisAccessGenerator(this, token, inInitializer));
push(new ThisAccessGenerator(
this, token, inInitializer, inFieldInitializer));
} else {
push(new IncompleteErrorGenerator(
this, token, null, fasta.messageThisAsIdentifier));
@ -3142,7 +3156,9 @@ abstract class BodyBuilder extends ScopeListener<JumpTarget>
if (context.isScopeReference && isInstanceContext) {
Member member = this.member.target;
member.transformerFlags |= TransformerFlag.superCalls;
push(new ThisAccessGenerator(this, token, inInitializer, isSuper: true));
push(new ThisAccessGenerator(
this, token, inInitializer, inFieldInitializer,
isSuper: true));
} else {
push(new IncompleteErrorGenerator(
this, token, null, fasta.messageSuperAsIdentifier));

View file

@ -17,11 +17,12 @@ part of 'kernel_expression_generator.dart';
class ThisAccessGenerator extends KernelGenerator {
final bool isInitializer;
final bool inFieldInitializer;
final bool isSuper;
ThisAccessGenerator(
ExpressionGeneratorHelper helper, Token token, this.isInitializer,
ThisAccessGenerator(ExpressionGeneratorHelper helper, Token token,
this.isInitializer, this.inFieldInitializer,
{this.isSuper: false})
: super(helper, token);
@ -34,7 +35,11 @@ class ThisAccessGenerator extends KernelGenerator {
Expression buildSimpleRead() {
if (!isSuper) {
return forest.thisExpression(token);
if (inFieldInitializer) {
return buildFieldInitializerError();
} else {
return forest.thisExpression(token);
}
} else {
return new SyntheticExpressionJudgment(helper.buildCompileTimeError(
messageSuperAsExpression,
@ -47,10 +52,10 @@ class ThisAccessGenerator extends KernelGenerator {
Expression buildFieldInitializerError() {
String keyword = isSuper ? "super" : "this";
int offset = offsetForToken(token);
return helper.buildCompileTimeError(
return helper.buildCompileTimeErrorExpression(
templateThisOrSuperAccessInFieldInitializer.withArguments(keyword),
offset,
keyword.length);
length: keyword.length);
}
buildPropertyAccess(
@ -65,6 +70,9 @@ class ThisAccessGenerator extends KernelGenerator {
}
return buildConstructorInitializer(offset, name, arguments);
}
if (inFieldInitializer && !isInitializer) {
return buildFieldInitializerError();
}
Member getter = helper.lookupInstanceMember(name, isSuper: isSuper);
if (send is SendAccessGenerator) {
// Notice that 'this' or 'super' can't be null. So we can ignore the

View file

@ -128,7 +128,7 @@ main() {
KernelBodyBuilder helper = new KernelBodyBuilder(
libraryBuilder, null, null, null, null, null, null, false, uri, null);
Generator generator = new ThisAccessGenerator(helper, token, false);
Generator generator = new ThisAccessGenerator(helper, token, false, false);
Library library = new Library(uri);
Class cls = new Class();
@ -203,7 +203,7 @@ main() {
new KernelLoadLibraryGenerator(helper, token, loadLibraryBuilder));
check(
"ThisAccessGenerator(offset: 4, isInitializer: false, isSuper: false)",
new ThisAccessGenerator(helper, token, false));
new ThisAccessGenerator(helper, token, false, false));
check("IncompleteErrorGenerator(offset: 4, message: Unspecified)",
new IncompleteErrorGenerator(helper, token, getter, message));
check("SendAccessGenerator(offset: 4, name: bar, arguments: (\"arg\"))",

View file

@ -15,7 +15,6 @@ WebPlatformTest/*: Skip # TODO(ahe): Make dart:html available.
[ $compiler != dart2js && $fasta && !$strong ]
Language/Classes/Constructors/Constant_Constructors/initializer_not_a_constant_t03: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t05: MissingCompileTimeError
Language/Expressions/Constants/bitwise_operators_t02: MissingCompileTimeError
Language/Expressions/Constants/exception_t04: MissingCompileTimeError
Language/Expressions/Constants/literal_number_t01: CompileTimeError
@ -26,7 +25,6 @@ Language/Expressions/Constants/math_operators_t04: MissingCompileTimeError
Language/Expressions/Constants/math_operators_t06: CompileTimeError
Language/Expressions/Numbers/static_type_of_int_t01: CompileTimeError
Language/Expressions/Numbers/syntax_t10: CompileTimeError
Language/Expressions/This/placement_t04: MissingCompileTimeError
LibTest/core/double/isInfinite_A01_t03: CompileTimeError
LibTest/core/int/abs_A01_t01: CompileTimeError
LibTest/core/int/ceilToDouble_A01_t01: CompileTimeError
@ -175,11 +173,6 @@ Language/Classes/Constructors/Factories/return_wrong_type_t04: CompileTimeError
Language/Classes/Constructors/Factories/syntax_t01: CompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_of_an_initializer_t03: CompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_of_an_initializer_t04: CompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t04: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t05: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t06: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t07: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t12: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/explicit_type_t01: CompileTimeError
Language/Classes/Constructors/Generative_Constructors/explicit_type_t02: CompileTimeError
Language/Classes/Constructors/Generative_Constructors/initializers_t15: CompileTimeError
@ -654,7 +647,6 @@ Language/Expressions/Method_Invocation/Super_Invocation/getter_lookup_failed_t01
Language/Expressions/Method_Invocation/Super_Invocation/getter_lookup_failed_t02: CompileTimeError
Language/Expressions/Method_Invocation/Super_Invocation/getter_lookup_failed_t03: CompileTimeError
Language/Expressions/Method_Invocation/Super_Invocation/getter_lookup_failed_t04: CompileTimeError
Language/Expressions/Method_Invocation/Super_Invocation/invocation_t02: MissingCompileTimeError
Language/Expressions/Method_Invocation/Super_Invocation/method_lookup_failed_t02: CompileTimeError
Language/Expressions/Method_Invocation/Super_Invocation/static_type_dynamic_t02: CompileTimeError
Language/Expressions/Method_Invocation/Super_Invocation/wrong_type_t01: CompileTimeError
@ -804,7 +796,6 @@ Language/Expressions/Strings/static_type_t06: CompileTimeError
Language/Expressions/Strings/static_type_t07: CompileTimeError
Language/Expressions/Strings/static_type_t08: CompileTimeError
Language/Expressions/Strings/static_type_t09: CompileTimeError
Language/Expressions/This/placement_t04: MissingCompileTimeError
Language/Expressions/This/static_type_t01: CompileTimeError
Language/Expressions/Throw/throw_various_types_t01: CompileTimeError
Language/Expressions/Type_Cast/evaluation_t04: CompileTimeError
@ -1695,10 +1686,6 @@ Language/Classes/Constructors/Factories/const_modifier_t01: MissingCompileTimeEr
Language/Classes/Constructors/Factories/const_modifier_t02: MissingCompileTimeError
Language/Classes/Constructors/Factories/default_value_t01: MissingCompileTimeError
Language/Classes/Constructors/Factories/default_value_t02: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t04: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t06: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t07: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t12: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/superclass_constructor_t02: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/superclass_constructor_t03: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/superclass_constructor_t05: MissingCompileTimeError
@ -1727,7 +1714,6 @@ Language/Expressions/Maps/constant_map_t02: MissingCompileTimeError
Language/Expressions/Maps/key_value_equals_operator_t01: MissingCompileTimeError
Language/Expressions/Method_Invocation/Ordinary_Invocation/object_method_invocation_t01: MissingCompileTimeError
Language/Expressions/Method_Invocation/Ordinary_Invocation/object_method_invocation_t02: MissingCompileTimeError
Language/Expressions/Method_Invocation/Super_Invocation/invocation_t02: MissingCompileTimeError
Language/Expressions/Method_Invocation/Super_Invocation/invocation_t03: MissingCompileTimeError
Language/Expressions/Numbers/syntax_t06: CompileTimeError
Language/Expressions/Numbers/syntax_t09: CompileTimeError

View file

@ -25,11 +25,6 @@ Language/Classes/Constructors/Factories/default_value_t01: MissingCompileTimeErr
Language/Classes/Constructors/Factories/default_value_t02: MissingCompileTimeError
Language/Classes/Constructors/Factories/function_type_t01: MissingCompileTimeError
Language/Classes/Constructors/Factories/function_type_t02: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t04: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t05: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t06: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t07: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/execution_t12: MissingCompileTimeError
Language/Classes/Constructors/Generative_Constructors/initializers_t15: CompileTimeError
Language/Classes/Constructors/name_t01: MissingCompileTimeError # Legal, see #33235
Language/Classes/Constructors/name_t02: MissingCompileTimeError # Legal, see #33235
@ -123,7 +118,6 @@ Language/Expressions/Maps/key_value_equals_operator_t01: MissingCompileTimeError
Language/Expressions/Method_Invocation/Ordinary_Invocation/accessible_instance_member_t04: CompileTimeError
Language/Expressions/Method_Invocation/Ordinary_Invocation/function_type_t01: MissingCompileTimeError
Language/Expressions/Method_Invocation/Super_Invocation/getter_lookup_failed_t02: CompileTimeError
Language/Expressions/Method_Invocation/Super_Invocation/invocation_t02: MissingCompileTimeError
Language/Expressions/Multiplicative_Expressions/syntax_t01: CompileTimeError
Language/Expressions/Postfix_Expressions/syntax_t01: CompileTimeError
Language/Expressions/Property_Extraction/Getter_Access_and_Method_Extraction/instance_of_type_getter_t01: CompileTimeError
@ -137,7 +131,6 @@ Language/Expressions/Relational_Expressions/syntax_t01: CompileTimeError
Language/Expressions/Shift/syntax_t01: CompileTimeError
Language/Expressions/Strings/String_Interpolation/double_quote_t02: CompileTimeError
Language/Expressions/Strings/String_Interpolation/single_quote_t02: CompileTimeError
Language/Expressions/This/placement_t04: MissingCompileTimeError
Language/Expressions/Unary_Expressions/syntax_t10: CompileTimeError
Language/Expressions/Unary_Expressions/syntax_t27: CompileTimeError
Language/Functions/Formal_Parameters/Optional_Formals/default_value_t01: MissingCompileTimeError

View file

@ -35,9 +35,6 @@ kill_test: RuntimeError # Uppercase constants removed
browser/package_resolve_browser_hook_test: SkipByDesign # Test written in a way that violates CSP.
deferred_in_isolate2_test: Skip # Issue 16898. Deferred loading does not work from an isolate in CSP-mode
[ $fasta ]
compile_time_error_test/01: MissingCompileTimeError
[ $jscl ]
spawn_uri_multi_test/none: RuntimeError # Issue 13544

View file

@ -316,7 +316,6 @@ constructor5_test: RuntimeError
constructor6_test: RuntimeError
constructor_call_as_function_test/01: MissingRuntimeError
constructor_named_arguments_test/none: RuntimeError
constructor_redirect_test/01: Crash # Assertion failure: Cannot find value Instance of 'ThisLocal' in (local(A.named2#x), local(A.named2#y), local(A.named2#z)) for j:constructor(A.named2).
deferred_constraints_constants_test/none: RuntimeError
deferred_constraints_constants_test/reference_after_load: RuntimeError
deferred_inheritance_constraints_test/extends: MissingCompileTimeError

View file

@ -28,7 +28,6 @@ const_instance_field_test/01: MissingCompileTimeError
const_map2_test/00: MissingCompileTimeError
const_map3_test/00: MissingCompileTimeError
const_switch2_test/01: MissingCompileTimeError
constructor_redirect_test/01: MissingCompileTimeError
deferred_inheritance_constraints_test/extends: MissingCompileTimeError
deferred_inheritance_constraints_test/implements: MissingCompileTimeError
deferred_inheritance_constraints_test/mixin: MissingCompileTimeError

View file

@ -242,7 +242,6 @@ constructor_duplicate_final_test/01: MissingCompileTimeError
constructor_duplicate_final_test/02: MissingCompileTimeError
constructor_named_arguments_test/01: MissingCompileTimeError
constructor_named_arguments_test/none: RuntimeError
constructor_redirect_test/01: Crash # Assertion failure: Cannot find value Instance of 'ThisLocal' in (local(A.named2#x), local(A.named2#y), local(A.named2#z)) for j:constructor(A.named2).
covariant_override/runtime_check_test: RuntimeError
covariant_subtyping_test: RuntimeError
deferred_constraints_type_annotation_test/as_operation: MissingCompileTimeError
@ -418,7 +417,6 @@ regress_28217_test/none: MissingCompileTimeError
regress_28255_test: RuntimeError
regress_28341_test: RuntimeError
regress_29405_test: RuntimeError
regress_29784_test/02: MissingCompileTimeError # Issue 29784
regress_30339_test: RuntimeError # Issue 26429
regress_31057_test: Crash # Unsupported operation: Unsupported type parameter type node B.
stacktrace_demangle_ctors_test: RuntimeError # Issue 12698
@ -755,7 +753,6 @@ const_types_test/34: MissingCompileTimeError
const_types_test/39: MissingCompileTimeError
constructor12_test: RuntimeError
constructor_named_arguments_test/none: RuntimeError
constructor_redirect_test/01: Crash # Assertion failure: Cannot find value Instance of 'ThisLocal' in (local(A.named2#x), local(A.named2#y), local(A.named2#z)) for j:constructor(A.named2).
covariant_subtyping_test: Crash
ct_const_test: RuntimeError
deferred_inheritance_constraints_test/extends: MissingCompileTimeError
@ -915,8 +912,6 @@ regress_27617_test/1: Crash # Assertion failure: Unexpected constructor j:constr
regress_28255_test: RuntimeError
regress_29025_test: CompileTimeError
regress_29405_test: CompileTimeError
regress_29784_test/01: Crash
regress_29784_test/02: MissingCompileTimeError # Issue 29784
regress_30339_test: CompileTimeError
setter_no_getter_test/01: CompileTimeError
stacktrace_demangle_ctors_test: RuntimeError # Issue 12698

View file

@ -248,7 +248,6 @@ const_syntax_test/05: MissingCompileTimeError
const_types_test/34: MissingCompileTimeError
const_types_test/39: MissingCompileTimeError
constants_test/05: MissingCompileTimeError
constructor_redirect_test/01: MissingCompileTimeError
covariant_subtyping_test: RuntimeError
cyclic_type_variable_test/01: MissingCompileTimeError
cyclic_type_variable_test/02: MissingCompileTimeError
@ -415,8 +414,6 @@ regress_23408_test: CompileTimeError # Issue 31533
regress_24283_test: RuntimeError # Expect.equals(expected: <-1>, actual: <4294967295>) fails.
regress_29025_test: CompileTimeError
regress_29405_test: CompileTimeError # Issue 31402 Error: A value of type '#lib2::Foo' can't be assigned to a variable of type '(#lib2::Foo) → void'.
regress_29784_test/01: MissingCompileTimeError
regress_29784_test/02: MissingCompileTimeError
regress_30339_test: RuntimeError # Uncaught Expect.isTrue(false) fails.
regress_30339_test: CompileTimeError
setter_no_getter_test/01: CompileTimeError

View file

@ -226,7 +226,6 @@ const_constructor_mixin_test: CompileTimeError # Issue 33644.
const_instance_field_test/01: MissingCompileTimeError # Fasta bug: Const instance field. Issue 32326.
const_types_test/34: MissingCompileTimeError # Issue 32988
const_types_test/39: MissingCompileTimeError # Issue 32988
constructor_redirect_test/01: MissingCompileTimeError # Fasta bug: Initializer refers to this.
cyclic_type_variable_test/01: MissingCompileTimeError # Issue 32989 (missing cycle check in bounds)
cyclic_type_variable_test/02: MissingCompileTimeError # Issue 32989 (missing cycle check in bounds)
cyclic_type_variable_test/03: MissingCompileTimeError # Issue 32989 (missing cycle check in bounds)
@ -322,8 +321,6 @@ redirecting_factory_default_values_test/01: MissingCompileTimeError # Fasta bug:
redirecting_factory_default_values_test/02: MissingCompileTimeError # Fasta bug: Default values are not allowed on redirecting factory constructors.
regress_22976_test/*: CompileTimeError # Issue 31935
regress_27617_test/1: MissingCompileTimeError # Fasta bug: Bad constructor redirection.
regress_29784_test/01: MissingCompileTimeError
regress_29784_test/02: MissingCompileTimeError
syntax_test/28: MissingCompileTimeError # Issue 29763
syntax_test/29: MissingCompileTimeError # Issue 29763
syntax_test/30: MissingCompileTimeError # Issue 29763

View file

@ -0,0 +1,64 @@
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
class A {
static var s = "something";
var a = "anything";
var x;
foo() => null;
bar(var y) => y;
static sfoo() => null;
static sbar(var y) => y;
A.next();
A(); //# 00: ok
A() : this.next(); //# 01: ok
A() : x = s; //# 02: ok
A() : x = sfoo(); //# 03: ok
A() : x = sbar(null); //# 04: ok
A() : x = a; //# 05: compile-time error
A() : x = foo(); //# 06: compile-time error
A() : x = bar(null); //# 07: compile-time error
A() : x = bar(this); //# 08: compile-time error
A() : x = this; //# 09: compile-time error
A() : x = this.a; //# 10: compile-time error
A() : x = this.foo(); //# 11: compile-time error
A() : x = this.bar(null); //# 12: compile-time error
A() : x = this.bar(this); //# 13: compile-time error
A() : x = sbar(this); //# 14: compile-time error
A() : x = sbar(this.a); //# 15: compile-time error
A() : x = sbar(this.foo()); //# 16: compile-time error
A() : x = sbar(this.bar(null)); //# 17: compile-time error
A() : x = sbar(this.bar(this)); //# 18: compile-time error
A() : this.x = (() => null); //# 19: ok
A() : this.x = (() => s)(); //# 20: ok
A() : this.x = ((c) => c); //# 21: ok
A() : this.x = ((c) => s)(null); //# 22: ok
A() : this.x = ((c) => c)(this); //# 23: compile-time error
A() : this.x = (() => this); //# 24: compile-time error
A() : this.x = (() => this.a)(); //# 25: compile-time error
A() : this.x = ((c) => this.foo()); //# 26: compile-time error
A() : this.x = ((c) => a)(null); //# 27: compile-time error
A() : this.x = ((c) => foo())(s); //# 28: compile-time error
A() : this.x = sbar((() { return null; })); //# 29: ok
A() : this.x = sbar((() { return s; })()); //# 30: ok
A() : this.x = sbar(((c) { return c; })); //# 31: ok
A() : this.x = sbar(((c) { return s; })(null)); //# 32: ok
A() : this.x = sbar(((c) { return c; })(this)); //# 33: compile-time error
A() : this.x = sbar((() { return this; })); //# 34: compile-time error
A() : this.x = sbar((() { return this.a; })()); //# 35: compile-time error
A() : this.x = sbar(((c) { return this.foo(); })); //# 36: compile-time error
A() : this.x = sbar(((c) { return a; })(null)); //# 37: compile-time error
A() : this.x = sbar(((c) { return foo(); })(s)); //# 38: compile-time error
A() : this.x = (s = null); //# 39: ok
A() : this.x = (a = null); //# 40: compile-time error
A() : this.x = (this.a = null); //# 41: compile-time error
}
main() {}

View file

@ -28,7 +28,6 @@ isolate/browser/*: Skip # TODO(ahe): Make dart:html available.
js/*: Skip # TODO(ahe): Make dart:js available.
[ $fasta ]
isolate/compile_time_error_test/01: MissingCompileTimeError
mirrors/deferred_constraints_constants_test/default_argument2: MissingCompileTimeError
mirrors/generic_bounded_by_type_parameter_test/02: MissingCompileTimeError
mirrors/generic_bounded_test/01: MissingCompileTimeError