diff --git a/src/dotty/tools/dotc/typer/Implicits.scala b/src/dotty/tools/dotc/typer/Implicits.scala index 6cb1f1271fce..e3dd113c2cf5 100644 --- a/src/dotty/tools/dotc/typer/Implicits.scala +++ b/src/dotty/tools/dotc/typer/Implicits.scala @@ -402,7 +402,8 @@ trait Implicits { self: Typer => || (to isRef defn.ObjectClass) || (to isRef defn.UnitClass) || (from.tpe isRef defn.NothingClass) - || (from.tpe isRef defn.NullClass)) NoImplicitMatches + || (from.tpe isRef defn.NullClass) + || (from.tpe eq NoPrefix)) NoImplicitMatches else try inferImplicit(to.stripTypeVar.widenExpr, from, from.pos) catch { diff --git a/src/dotty/tools/dotc/typer/TypeAssigner.scala b/src/dotty/tools/dotc/typer/TypeAssigner.scala index 42ee513cd8d1..ac46ee7232b3 100644 --- a/src/dotty/tools/dotc/typer/TypeAssigner.scala +++ b/src/dotty/tools/dotc/typer/TypeAssigner.scala @@ -8,6 +8,7 @@ import Scopes._, Contexts._, Constants._, Types._, Symbols._, Names._, Flags._, import ErrorReporting._, Annotations._, Denotations._, SymDenotations._, StdNames._, TypeErasure._ import util.Positions._ import config.Printers._ +import NameOps._ trait TypeAssigner { import tpd._ @@ -16,7 +17,11 @@ trait TypeAssigner { * @param packageOk The qualifier may refer to a package. */ def qualifyingClass(tree: untpd.Tree, qual: Name, packageOK: Boolean)(implicit ctx: Context): Symbol = { - def qualifies(sym: Symbol) = sym.isClass && (qual.isEmpty || sym.name == qual) + def qualifies(sym: Symbol) = + sym.isClass && ( + qual.isEmpty || + sym.name == qual || + sym.is(Module) && sym.name.stripModuleClassSuffix == qual) ctx.outersIterator.map(_.owner).find(qualifies) match { case Some(c) if packageOK || !(c is Package) => c diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index dd75e960e5ef..6a2ff30fa7c2 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -1068,7 +1068,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit val packageContext = if (pkg is Package) ctx.fresh.setOwner(pkg.moduleClass).setTree(tree) else { - ctx.error(d"$pkg is not a package", tree.pos) + ctx.error(d"$pkg is already defined, cannot be a package", tree.pos) ctx } val stats1 = typedStats(tree.stats, pkg.moduleClass)(packageContext) diff --git a/test/dotc/tests.scala b/test/dotc/tests.scala index 3610482364ea..f9860b4aac00 100644 --- a/test/dotc/tests.scala +++ b/test/dotc/tests.scala @@ -155,6 +155,7 @@ class tests extends CompilerTest { @Test def neg_i0091_infpaths = compileFile(negDir, "i0091-infpaths", xerrors = 3) @Test def neg_i0248_inherit_refined = compileFile(negDir, "i0248-inherit-refined", xerrors = 4) @Test def neg_i0281 = compileFile(negDir, "i0281-null-primitive-conforms", xerrors = 3) + @Test def neg_i324 = compileFile(negDir, "i324", xerrors = 2) @Test def neg_i583 = compileFile(negDir, "i0583-skolemize", xerrors = 2) @Test def neg_i941 = compileFile(negDir, "i941", xerrors = 3) @Test def neg_finalSealed = compileFile(negDir, "final-sealed", xerrors = 2) diff --git a/tests/neg/i324.scala b/tests/neg/i324.scala new file mode 100644 index 000000000000..1d03a4d022c4 --- /dev/null +++ b/tests/neg/i324.scala @@ -0,0 +1,5 @@ +class O +object O { + val x: this.type = OO.this + val y: O = OO.this +} diff --git a/tests/pos/i324.scala b/tests/pos/i324.scala new file mode 100644 index 000000000000..5461379bebdb --- /dev/null +++ b/tests/pos/i324.scala @@ -0,0 +1,7 @@ +class O +object O { + val x: this.type = O.this +} +object O2 { + val x: this.type = O2.this +}