mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-15 07:43:26 +00:00
more informative error msg for undeclared field (A(badfield: 1) and a.badfield = expr) (#17777)
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
40
tests/errmsgs/tundeclared_field.nim
Normal file
40
tests/errmsgs/tundeclared_field.nim
Normal 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]
|
||||
Reference in New Issue
Block a user