Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make 'field is not accessible' and 'field initialized twice' errors point to the field inside the obj construction #24557

Merged
merged 5 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/semobjconstr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/objects/mobjconstr_msgs.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type
PrivateField* = object
priv: string
23 changes: 23 additions & 0 deletions tests/objects/tobjconstr_msgs.nim
Original file line number Diff line number Diff line change
@@ -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']#
)