Skip to content

Commit 50e1768

Browse files
committed
Reject method declarations on interface types
Interfaces are abstract and cannot be receivers. The compiler was silently accepting the declaration and storing the method as an unreachable global — the runtime would never dispatch to it because interface values are always their concrete underlying type. Add a registry check in methodDecl that returns MethodOnInterface before the function body is compiled.
1 parent b17b19a commit 50e1768

3 files changed

Lines changed: 4 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MethodOnInterface
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type Shape interface { area() float }
2+
func (s Shape) fval() float { return 0.0 }

‎src/lang/compiler.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,7 @@ pub const Compiler = struct {
10661066
if (self.cur.typ != .ident) return error.UnexpectedToken;
10671067
const recv_type = self.cur.src;
10681068
self.advance();
1069+
if (self.registry.hasInterfaceType(recv_type)) return error.MethodOnInterface;
10691070
try self.consume(.rparen);
10701071
if (self.cur.typ != .ident) return error.UnexpectedToken;
10711072
const method_name = self.cur.src;

0 commit comments

Comments
 (0)