Skip to content

Commit

Permalink
Semantic: duplicate fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Mar 30, 2024
1 parent 299fad1 commit dec6df0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/semantic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,13 @@ const checkVariant = (variant: Variant, ctx: Context) => {

const module = ctx.moduleStack.at(-1)!
const typeDefScope = <TypeDefScope>module.scopeStack.at(-1)!
variant.fieldDefs.forEach(fieldDef => {
variant.fieldDefs.forEach((fieldDef, i) => {
checkType(fieldDef.fieldType, ctx)
fieldDef.type = typeToVirtual(fieldDef.fieldType, ctx)
// TODO: check duplicate field defs

if (variant.fieldDefs.slice(0, i).some(f => f.name.value === fieldDef.name.value)) {
addError(ctx, duplicateError(ctx, fieldDef.name, fieldDef.name.value, 'field'))
}
})

const generics = [...typeDefScope.definitions.values()].map(d => <Generic>d).map(g => genericToVirtual(g, ctx))
Expand Down
6 changes: 6 additions & 0 deletions src/semantic/semantic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,12 @@ type Foo {
const ctx = check(code)
expect(ctx.errors.map(e => e.message)).toEqual(['duplicate variant `A`'])
})

it('duplicate field', () => {
const code = `type Foo(a: Int, a: String)`
const ctx = check(code)
expect(ctx.errors.map(e => e.message)).toEqual(['duplicate field `a`'])
})
})

describe('fn def', () => {
Expand Down

0 comments on commit dec6df0

Please sign in to comment.