Skip to content

Commit

Permalink
Semantic: no body fn error
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Mar 17, 2024
1 parent 55fa335 commit 4a1a5fc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/semantic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,16 @@ const checkFnDef = (fnDef: FnDef, ctx: Context): void => {
upcast(rs, rs.type!, returnTypeResolved, ctx)
})
} else {
if (!module.compiled && instScope?.rel.instanceDef.kind !== 'trait-def') {
if (!instScope) {
addWarning(ctx, noBodyFnError(ctx, fnDef))
} else {
if (instScope?.rel.instanceDef.kind !== 'trait-def') {
addError(ctx, noBodyFnError(ctx, fnDef))
}
}
if (instScope?.rel.instanceDef.kind === 'trait-def' && fnDef.pub) {
addWarning(ctx, unnecessaryPubMethodError(ctx, fnDef))
}
}
if (instScope?.rel.instanceDef.kind === 'trait-def' && fnDef.pub) {
addWarning(ctx, unnecessaryPubMethodError(ctx, fnDef))
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/semantic/semantic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,4 +701,28 @@ fn main() {
expect(ctx.errors.map(e => e.message)).toEqual(['missing variable initialization'])
})
})

describe('fn def', () => {
it('fn no body', () => {
const code = `\
type Node<T> {
Node(node: Node<T>),
Leaf(t: T)
}
impl <T> Node<T> {
fn child()
}
fn main() {
let n = Node(Node(Leaf(5)))
n.child().child()
}`
const ctx = check(code)
expect(ctx.errors.map(e => e.message)).toEqual([
'fn `child` has no body',
'method `std::unit::Unit::child` not found'
])
})
})
})
2 changes: 1 addition & 1 deletion src/typecheck/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { VirtualFnType, VirtualType } from './index'
import { holeType, selfType } from './type'

export const makeFnGenericMap = (fnType: VirtualFnType, argTypes: VirtualType[]): Map<string, VirtualType> => {
assert(argTypes.length <= fnType.paramTypes.length, 'fn args > params')
if (argTypes.length > fnType.paramTypes.length) return new Map()
return argTypes
.map((argType, i) => {
const param = fnType.paramTypes[i]
Expand Down

0 comments on commit 4a1a5fc

Please sign in to comment.