From 10ca1783d45dc9c87ea0c09f4ea046ac539f1749 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Thu, 6 Feb 2014 13:00:09 +0100 Subject: SI-8207 Allow import qualified by self reference This regressed in SI-6815 / #2374. We check if the result of `typedQualifier(Ident(selfReference))` is a stable identifier pattern. But we actually see the expansion to `C.this`, which doesn't qualify. This commit adds a special cases to `importSig` to compensate. This is safe enough, because the syntax prevents the following: scala> class C { import C.this.toString } :1: error: '.' expected but '}' found. class C { import C.this.toString } ^ So loosening the check here doesn't admit invalid programs. I've backed this up with a `neg` test. The enclosed test also checks that we can use the self reference in a singleton type, and as a qualifier in a type selection (These weren't actually broken.) Maybe it would be more principled to avoid expanding the self reference in `typedIdent`. I can imagine that the current situation is a pain for refactoring tools that try to implement a rename refactoring, for example. Seems a bit risky at the minute, but I've noted the idea in a comment. --- src/compiler/scala/tools/nsc/typechecker/Namers.scala | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/compiler/scala/tools/nsc/typechecker/Namers.scala') diff --git a/src/compiler/scala/tools/nsc/typechecker/Namers.scala b/src/compiler/scala/tools/nsc/typechecker/Namers.scala index 27e8698676..8c29c8d242 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Namers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Namers.scala @@ -1407,8 +1407,14 @@ trait Namers extends MethodSynthesis { if (expr1.isErrorTyped) ErrorType else { - if (!treeInfo.isStableIdentifierPattern(expr1)) - typer.TyperErrorGen.UnstableTreeError(expr1) + expr1 match { + case This(_) => + // SI-8207 okay, typedIdent expands Ident(self) to C.this which doesn't satisfy the next case + // TODO should we change `typedIdent` not to expand to the `Ident` to a `This`? + case _ if treeInfo.isStableIdentifierPattern(expr1) => + case _ => + typer.TyperErrorGen.UnstableTreeError(expr1) + } val newImport = treeCopy.Import(imp, expr1, selectors).asInstanceOf[Import] checkSelectors(newImport) -- cgit v1.2.3