more informative error msg for undeclared field (A(badfield: 1) and a.badfield = expr) (#17777)

This commit is contained in:
Timothee Cour
2021-05-16 10:03:22 -07:00
committed by GitHub
parent 65f6b66820
commit 3619a5a2aa
3 changed files with 55 additions and 3 deletions

View File

@@ -411,8 +411,19 @@ proc resolveOverloads(c: PContext, n, orig: PNode,
if overloadsState == csEmpty and result.state == csEmpty:
if efNoUndeclared notin flags: # for tests/pragmas/tcustom_pragma.nim
# xxx adapt/use errorUndeclaredIdentifierHint(c, n, f.ident)
localError(c.config, n.info, getMsgDiagnostic(c, flags, n, f))
template impl() =
# xxx adapt/use errorUndeclaredIdentifierHint(c, n, f.ident)
localError(c.config, n.info, getMsgDiagnostic(c, flags, n, f))
if n[0].kind == nkIdent and n[0].ident.s == ".=" and n[2].kind == nkIdent:
let sym = n[1].typ.sym
if sym == nil:
impl()
else:
let field = n[2].ident.s
let msg = errUndeclaredField % field & " for type " & getProcHeader(c.config, sym)
localError(c.config, orig[2].info, msg)
else:
impl()
return
elif result.state != csMatch:
if nfExprCall in n.flags:

View File

@@ -429,7 +429,8 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags): PNode =
hasError = true
break
# 2) No such field exists in the constructed type
localError(c.config, field.info, errUndeclaredFieldX % id.s)
let msg = errUndeclaredField % id.s & " for type " & getProcHeader(c.config, t.sym)
localError(c.config, field.info, msg)
hasError = true
break

View File

@@ -0,0 +1,40 @@
discard """
cmd: '''nim check --hints:off $file'''
action: reject
nimout: '''
tundeclared_field.nim(25, 12) Error: undeclared field: 'bad' for type tundeclared_field.A [type declared in tundeclared_field.nim(22, 8)]
tundeclared_field.nim(30, 16) Error: undeclared field: 'bad' for type tundeclared_field.A [type declared in tundeclared_field.nim(28, 8)]
tundeclared_field.nim(36, 4) Error: undeclared field: 'bad' for type tundeclared_field.A [type declared in tundeclared_field.nim(33, 8)]
tundeclared_field.nim(40, 13) Error: cannot instantiate Foo [type declared in tundeclared_field.nim(39, 8)]
'''
"""
# line 20
block:
type A = object
a0: int
var a: A
discard a.bad
block:
type A = object
a0: int
var a = A(bad: 0)
block:
type A = object
a0: int
var a: A
a.bad = 0
block:
type Foo[T: SomeInteger] = object
var a: Foo[float]