Skip to content

Commit df011fa

Browse files
authored
Merge pull request #9799 from tudorv91/fix-#9432
Fix #9432: Fail compilation on @Native methods in traits
2 parents 8913374 + 541f957 commit df011fa

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
166166
TypeSpliceInValPatternID,
167167
ModifierNotAllowedForDefinitionID,
168168
CannotExtendJavaEnumID,
169-
InvalidReferenceInImplicitNotFoundAnnotationID
169+
InvalidReferenceInImplicitNotFoundAnnotationID,
170+
TraitMayNotDefineNativeMethodID
170171

171172
def errorNumber = ordinal - 2
172173
}

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+
-- [E159] 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)