adds legacy:typedescFieldAccess

This commit is contained in:
ringabout
2026-05-15 20:39:09 +08:00
parent 63933a3865
commit 1ce95381d4
4 changed files with 19 additions and 1 deletions

View File

@@ -12,6 +12,10 @@ rounding guarantees (via the
avoid conflicts with `system.default`, so named argument usage for this
parameter like `getOrDefault(..., default = ...)` will have to be changed.
- Typedesc field access on object/tuple types (e.g. `Foo[int].val`) is now
restricted to `typeof` context. Use `--legacy:typedescFieldAccess` to restore
the previous behavior of allowing it outside `typeof`.
- With `-d:nimPreviewCheckedClose`, the `close` function in the `std/syncio` module now raises an IO exception in case of an error.
- Unknown warnings and hints now gives warnings `warnUnknownNotes` instead of

View File

@@ -262,6 +262,9 @@ type
procParamTypeBackendAliases
## Keep the old proc type compatibility rules that ignore backend
## c type aliases.
typedescFieldAccess
## Allow typedesc field access on object/tuple types outside of
## typeof context.
SymbolFilesOption* = enum
disabledSf, writeOnlySf, readOnlySf, v2Sf, stressTest

View File

@@ -1500,7 +1500,7 @@ proc tryReadingTypeField(c: PContext, n: PNode, i: PIdent, ty: PType): PNode =
markUsed(c, n.info, f)
onUse(n.info, f)
of tyObject, tyTuple:
if c.inTypeofContext > 0 and
if (c.inTypeofContext > 0 or typedescFieldAccess in c.config.legacyFeatures) and
ty.n != nil and ty.n.kind == nkRecList:
let field = lookupInRecord(ty.n, i)
if field != nil:

View File

@@ -0,0 +1,11 @@
discard """
matrix: "--legacy:typedescFieldAccess"
output: "4"
"""
type
Foo[T] = object
val: T
var x: Foo[int].val = 4
echo x