Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jul 25, 2019
1 parent 978f6a8 commit 4d35793
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
2 changes: 2 additions & 0 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions lib/pure/sugar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 0 additions & 12 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions tests/magics/talias2.nim
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 4d35793

Please sign in to comment.