Skip to content

Fix #9432: Fail compilation on @native methods in traits #9799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
TypeSpliceInValPatternID,
ModifierNotAllowedForDefinitionID,
CannotExtendJavaEnumID,
InvalidReferenceInImplicitNotFoundAnnotationID
InvalidReferenceInImplicitNotFoundAnnotationID,
TraitMayNotDefineNativeMethodID

def errorNumber = ordinal - 2
}
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,13 @@ import ast.tpd
def explain = ""
}

class TraitMayNotDefineNativeMethod(sym: Symbol)(
implicit ctx: Context)
extends SyntaxMsg(TraitMayNotDefineNativeMethodID) {
def msg = em"""A trait cannot define a ${hl("@native")} method."""
def explain = ""
}

class OnlyClassesCanHaveDeclaredButUndefinedMembers(sym: Symbol)(
implicit ctx: Context)
extends SyntaxMsg(OnlyClassesCanHaveDeclaredButUndefinedMembersID) {
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ object Checking {
if (sym.hasAnnotation(defn.NativeAnnot)) {
if (!sym.is(Deferred))
fail(NativeMembersMayNotHaveImplementation(sym))
else if(sym.owner.is(Trait))
fail(TraitMayNotDefineNativeMethod(sym))
}
else if (sym.is(Deferred, butNot = Param) && !sym.isType && !sym.isSelfSym) {
if (!sym.owner.isClass || sym.owner.is(Module) || sym.owner.isAnonymousClass)
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i9432.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- [E159] Syntax Error: tests/neg/i9432.scala:2:14 ---------------------------------------------------------------------
2 | @native def foo(x: Int): Unit // error
| ^
| A trait cannot define a @native method.
3 changes: 3 additions & 0 deletions tests/neg/i9432.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trait CLibrary {
@native def foo(x: Int): Unit // error
}