mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
Make 'field is not accessible' and 'field initialized twice' errors point to the field inside the obj construction (#24557)
Fixes two line infos to make the error's clearer inside editors
- 'field is not accessible' would point to the whole object construction
instead of just the field inside the construction
- 'field initialized twice' would point to the colon instead of the
field
(cherry picked from commit 6bc52737b3)
This commit is contained in:
@@ -79,7 +79,7 @@ proc semConstrField(c: PContext, flags: TExprFlags,
|
||||
if nfSkipFieldChecking in assignment[1].flags:
|
||||
discard
|
||||
elif not fieldVisible(c, field):
|
||||
localError(c.config, initExpr.info,
|
||||
localError(c.config, assignment[0].info,
|
||||
"the field '$1' is not accessible." % [field.name.s])
|
||||
return
|
||||
|
||||
@@ -521,7 +521,7 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType
|
||||
for j in 1..<i:
|
||||
let prevId = considerQuotedIdent(c, result[j][0])
|
||||
if prevId.id == id.id:
|
||||
localError(c.config, field.info, errFieldInitTwice % id.s)
|
||||
localError(c.config, field[0].info, errFieldInitTwice % id.s)
|
||||
hasError = true
|
||||
break
|
||||
# 2) No such field exists in the constructed type
|
||||
|
||||
3
tests/objects/mobjconstr_msgs.nim
Normal file
3
tests/objects/mobjconstr_msgs.nim
Normal file
@@ -0,0 +1,3 @@
|
||||
type
|
||||
PrivateField* = object
|
||||
priv: string
|
||||
23
tests/objects/tobjconstr_msgs.nim
Normal file
23
tests/objects/tobjconstr_msgs.nim
Normal file
@@ -0,0 +1,23 @@
|
||||
discard """
|
||||
cmd: "nim check $file"
|
||||
"""
|
||||
|
||||
import mobjconstr_msgs
|
||||
|
||||
|
||||
block:
|
||||
discard PrivateField(
|
||||
priv: "test" #[tt.Error
|
||||
^ the field 'priv' is not accessible]#
|
||||
)
|
||||
|
||||
|
||||
block:
|
||||
type
|
||||
Foo = object
|
||||
field: string
|
||||
discard Foo(
|
||||
field: "test",
|
||||
field: "test" #[tt.Error
|
||||
^ field initialized twice: 'field']#
|
||||
)
|
||||
Reference in New Issue
Block a user