Skip to content

Commit 5d90f67

Browse files
committed
Fix scala#9432: Fail compilation on @Native methods in traits
1 parent bd05d37 commit 5d90f67

File tree

5 files changed

+17
-0
lines changed

5 files changed

+17
-0
lines changed

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
7575
AbstractOverrideOnlyInTraitsID,
7676
TraitsMayNotBeFinalID,
7777
NativeMembersMayNotHaveImplementationID,
78+
TraitMayNotDefineNativeMethodID,
7879
OnlyClassesCanHaveDeclaredButUndefinedMembersID,
7980
CannotExtendAnyValID,
8081
CannotHaveSameNameAsID,

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,13 @@ import ast.tpd
14801480
def explain = ""
14811481
}
14821482

1483+
class TraitMayNotDefineNativeMethod(sym: Symbol)(
1484+
implicit ctx: Context)
1485+
extends SyntaxMsg(TraitMayNotDefineNativeMethodID) {
1486+
def msg = em"""A trait cannot define a ${hl("@native")} method."""
1487+
def explain = ""
1488+
}
1489+
14831490
class OnlyClassesCanHaveDeclaredButUndefinedMembers(sym: Symbol)(
14841491
implicit ctx: Context)
14851492
extends SyntaxMsg(OnlyClassesCanHaveDeclaredButUndefinedMembersID) {

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ object Checking {
467467
if (sym.hasAnnotation(defn.NativeAnnot)) {
468468
if (!sym.is(Deferred))
469469
fail(NativeMembersMayNotHaveImplementation(sym))
470+
else if(sym.owner.is(Trait))
471+
fail(TraitMayNotDefineNativeMethod(sym))
470472
}
471473
else if (sym.is(Deferred, butNot = Param) && !sym.isType && !sym.isSelfSym) {
472474
if (!sym.owner.isClass || sym.owner.is(Module) || sym.owner.isAnonymousClass)

tests/neg/i9432.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- [E067] Syntax Error: tests/neg/i9432.scala:2:14 ---------------------------------------------------------------------
2+
2 | @native def foo(x: Int): Unit // error
3+
| ^
4+
| A trait cannot define a @native method.

tests/neg/i9432.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
trait CLibrary {
2+
@native def foo(x: Int): Unit // error
3+
}

0 commit comments

Comments
 (0)