Skip to content

Commit 065243d

Browse files
authored
followup #17777: declaredloc field error msgs now work with generics (#18259)
* followup #17777: declaredloc field error msgs now work with generics * fix tests * cleanup
1 parent a266c54 commit 065243d

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

compiler/astmsgs.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
import std/strutils
33
import options, ast, msgs
44

5+
proc typSym*(t: PType): PSym =
6+
result = t.sym
7+
if result == nil and t.kind == tyGenericInst: # this might need to be refined
8+
result = t[0].sym
9+
510
proc addDeclaredLoc*(result: var string, conf: ConfigRef; sym: PSym) =
611
result.add " [$1 declared in $2]" % [sym.kind.toHumanStr, toFileLineCol(conf, sym.info)]
712

compiler/semcall.nim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ proc getMsgDiagnostic(c: PContext, flags: TExprFlags, n, f: PNode): string =
326326

327327
let ident = considerQuotedIdent(c, f, n).s
328328
if {nfDotField, nfExplicitCall} * n.flags == {nfDotField}:
329-
let sym = n[1].typ.sym
329+
let sym = n[1].typ.typSym
330330
var typeHint = ""
331331
if sym == nil:
332332
# Perhaps we're in a `compiles(foo.bar)` expression, or
@@ -337,7 +337,8 @@ proc getMsgDiagnostic(c: PContext, flags: TExprFlags, n, f: PNode): string =
337337
discard
338338
else:
339339
typeHint = " for type " & getProcHeader(c.config, sym)
340-
result = errUndeclaredField % ident & typeHint & " " & result
340+
let suffix = if result.len > 0: " " & result else: ""
341+
result = errUndeclaredField % ident & typeHint & suffix
341342
else:
342343
if result.len == 0: result = errUndeclaredRoutine % ident
343344
else: result = errBadRoutine % [ident, result]

tests/errmsgs/tundeclared_field.nim

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,48 @@ discard """
22
cmd: '''nim check --hints:off $file'''
33
action: reject
44
nimout: '''
5-
tundeclared_field.nim(25, 12) Error: undeclared field: 'bad' for type tundeclared_field.A [type declared in tundeclared_field.nim(22, 8)]
6-
tundeclared_field.nim(30, 16) Error: undeclared field: 'bad' for type tundeclared_field.A [type declared in tundeclared_field.nim(28, 8)]
7-
tundeclared_field.nim(36, 4) Error: undeclared field: 'bad' for type tundeclared_field.A [type declared in tundeclared_field.nim(33, 8)]
8-
tundeclared_field.nim(40, 13) Error: cannot instantiate Foo [type declared in tundeclared_field.nim(39, 8)]
5+
tundeclared_field.nim(25, 12) Error: undeclared field: 'bad1' for type tundeclared_field.A [type declared in tundeclared_field.nim(22, 8)]
6+
tundeclared_field.nim(30, 17) Error: undeclared field: 'bad2' for type tundeclared_field.A [type declared in tundeclared_field.nim(28, 8)]
7+
tundeclared_field.nim(36, 4) Error: undeclared field: 'bad3' for type tundeclared_field.A [type declared in tundeclared_field.nim(33, 8)]
8+
tundeclared_field.nim(42, 12) Error: undeclared field: 'bad4' for type tundeclared_field.B [type declared in tundeclared_field.nim(39, 8)]
9+
tundeclared_field.nim(43, 4) Error: undeclared field: 'bad5' for type tundeclared_field.B [type declared in tundeclared_field.nim(39, 8)]
10+
tundeclared_field.nim(44, 23) Error: undeclared field: 'bad6' for type tundeclared_field.B [type declared in tundeclared_field.nim(39, 8)]
11+
tundeclared_field.nim(46, 19) Error: undeclared field: 'bad7' for type tundeclared_field.B [type declared in tundeclared_field.nim(39, 8)]
12+
tundeclared_field.nim(50, 13) Error: cannot instantiate Foo [type declared in tundeclared_field.nim(49, 8)]
913
'''
1014
"""
1115

12-
13-
14-
15-
16-
17-
18-
19-
16+
#[
17+
xxx in future work, generic instantiations (e.g. `B[int]`) should be shown with their instantiation instead of `tundeclared_field.B`,
18+
maybe using TPreferedDesc.preferResolved or preferMixed
19+
]#
2020
# line 20
2121
block:
2222
type A = object
2323
a0: int
2424
var a: A
25-
discard a.bad
25+
discard a.bad1
2626

2727
block:
2828
type A = object
2929
a0: int
30-
var a = A(bad: 0)
30+
var a = A(bad2: 0)
3131

3232
block:
3333
type A = object
3434
a0: int
3535
var a: A
36-
a.bad = 0
36+
a.bad3 = 0
37+
38+
block:
39+
type B[T] = object
40+
b0: int
41+
var b: B[int]
42+
discard b.bad4
43+
b.bad5 = 0
44+
var b2 = B[int](bad6: 0)
45+
type Bi = B[int]
46+
var b3 = Bi(bad7: 0)
3747

3848
block:
3949
type Foo[T: SomeInteger] = object

0 commit comments

Comments
 (0)