From 4d357934d3b91a8bd7bcbdb48e108c445ee6106a Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Thu, 25 Jul 2019 15:54:11 -0700 Subject: [PATCH] address comments --- compiler/options.nim | 2 ++ compiler/semexprs.nim | 5 ++++- lib/pure/sugar.nim | 12 ++++++++++++ lib/system.nim | 12 ------------ tests/magics/talias2.nim | 3 +++ 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/compiler/options.nim b/compiler/options.nim index 6303224a2d201..90687094f2cca 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -134,6 +134,8 @@ type ## This requires building nim with `-d:nimHasLibFFI` ## which itself requires `nimble install libffi`, see #10150 ## Note: this feature can't be localized with {.push.} + aliasSym, + ## enable `Alias` magic to alias symbols SymbolFilesOption* = enum disabledSf, writeOnlySf, readOnlySf, v2Sf diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 41b6e28c3b676..2958ec9ee891b 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -2244,7 +2244,10 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode = of mSizeOf: result = semSizeof(c, setMs(n, s)) of mAlias: - result = semAlias(c, n, s, flags) + if aliasSym in c.features: + result = semAlias(c, n, s, flags) + else: + globalError(c.config, n.info, "requires --experimental:aliasSym") else: result = semDirectOp(c, n, flags) diff --git a/lib/pure/sugar.nim b/lib/pure/sugar.nim index c4c99121420f3..7a71b03a85123 100644 --- a/lib/pure/sugar.nim +++ b/lib/pure/sugar.nim @@ -238,3 +238,15 @@ macro distinctBase*(T: typedesc): untyped = while typeSym.typeKind == ntyDistinct: typeSym = getTypeImpl(typeSym)[0] typeSym.freshIdentNodes + +when defined(nimHasAlias): + proc aliasImpl[T1, T2](name: T1, expr: T2) {.magic: "Alias".} + + template `:=`*(name, expr) = + ## Declares `a` as alias of `expr`, which must resolve to a symbol. + runnableExamples: + echo2:=system.echo + echo2 "hello" + declared2:=system.declared + doAssert declared2(echo2) + aliasImpl(name, expr) diff --git a/lib/system.nim b/lib/system.nim index 025a442c554de..28eb1a1ef4bca 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -149,18 +149,6 @@ else: template runnableExamples*(body: untyped) = discard -when defined(nimHasAlias): - proc aliasImpl[T1, T2](name: T1, expr: T2) {.magic: "Alias".} - - template `:=`*(name, expr) = - ## Declares `a` as alias of `expr`, which must resolve to a symbol. - runnableExamples: - echo2:=system.echo - echo2 "hello" - declared2:=system.declared - doAssert declared2(echo2) - aliasImpl(name, expr) - proc declared*(x: untyped): bool {.magic: "Defined", noSideEffect, compileTime.} ## Special compile-time procedure that checks whether `x` is ## declared. `x` has to be an identifier or a qualified identifier. diff --git a/tests/magics/talias2.nim b/tests/magics/talias2.nim index 13905802f1c94..6a4602e82f081 100644 --- a/tests/magics/talias2.nim +++ b/tests/magics/talias2.nim @@ -1,4 +1,7 @@ import std/macros +import std/sugar + +{.push experimental: "aliasSym".} proc fun0(a: int): auto = $a template fun3(a: int): untyped = $a