diff --git a/changelog.md b/changelog.md index f87dadae63..304aeb1f7b 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/compiler/options.nim b/compiler/options.nim index 7a28b1dc6f..adc63e5687 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -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 diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index b9420893d5..57e0c86221 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -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: diff --git a/tests/metatype/ttypedesc_field_legacy.nim b/tests/metatype/ttypedesc_field_legacy.nim new file mode 100644 index 0000000000..96196c22a8 --- /dev/null +++ b/tests/metatype/ttypedesc_field_legacy.nim @@ -0,0 +1,11 @@ +discard """ + matrix: "--legacy:typedescFieldAccess" + output: "4" +""" + +type + Foo[T] = object + val: T + +var x: Foo[int].val = 4 +echo x