Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Unified Diff: pkg/compiler/lib/src/resolution/members.dart

Issue 1078873003: Report compile-time error if using `this` implicitly in initializers (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/co19/co19-dart2js.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/compiler/lib/src/resolution/members.dart
diff --git a/pkg/compiler/lib/src/resolution/members.dart b/pkg/compiler/lib/src/resolution/members.dart
index 652387b9ab146828326a32ce5cdb473311b9a15c..7aa3e1dcdf068bb5d1aceced24f1126deddaa1b7 100644
--- a/pkg/compiler/lib/src/resolution/members.dart
+++ b/pkg/compiler/lib/src/resolution/members.dart
@@ -2112,6 +2112,9 @@ class ResolverVisitor extends MappingVisitor<ResolutionResult> {
* This field is updated when nested closures are visited.
*/
Element enclosingElement;
+
+ /// Whether we are in a context where `this` is accessible (this will be false
+ /// in static contexts, factory methods, and field initializers).
bool inInstanceContext;
bool inCheckContext;
bool inCatchBlock;
@@ -2304,6 +2307,19 @@ class ResolverVisitor extends MappingVisitor<ResolutionResult> {
node, node.source, MessageKind.CANNOT_RESOLVE,
{'name': node});
registry.registerThrowNoSuchMethod();
+
+ // We also report an error within initializers because `this` is
+ // implicitly accessed when unqualified identifiers are not resolved.
+ // For details, see section 16.14.3 of the spec (2nd edition):
+ // An unqualified invocation `i` of the form `id(a1, ...)`
+ // ...
+ // If `i` does not occur inside a top level or static function, `i`
+ // is equivalent to `this.id(a1 , ...)`.
+ if ((enclosingElement.isInstanceMember && enclosingElement.isField) ||
+ (enclosingElement.isGenerativeConstructor)) {
+ compiler.reportError(
Johnni Winther 2015/04/10 07:55:50 From a users perspective, it might be better to ha
Siggi Cherem (dart-lang) 2015/04/10 18:56:26 I have it a try. WDYT?
+ node, MessageKind.NO_INSTANCE_AVAILABLE, {'name': node.source});
+ }
}
} else if (element.isErroneous) {
// Use the erroneous element.
« no previous file with comments | « no previous file | tests/co19/co19-dart2js.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698