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

type(x) != typeof(x): typeof(x) keeps the ntyAlias, unlike type(x) #16964

Open
1 of 5 tasks
timotheecour opened this issue Feb 8, 2021 · 1 comment
Open
1 of 5 tasks

Comments

@timotheecour
Copy link
Member

timotheecour commented Feb 8, 2021

after reviewing #16962 I tried to double check whether type(x) and typeof(x) have the same behavior (without the typeOfProc arg); it turns out it's not the case:

  • Example 1
when true:
  block:
    type outType = type(0.0)
    echo outType
  block:
    type outType = typeof(0.0)
    echo outType

Current Output

float64
outType

Expected Output

both should behave the same, and both should print float64:
float64
float64

note that this already prints:

when true:
  block:
    echo type(0.0)
  block:
    echo typeof(0.0)

float64
float64

  • Example 2
when true:
  import macros
  macro bar(T: typed) =
    let s = T.getTypeImpl[1]
    echo s.typeKind
  type outType1 = type(0.0)
  type outType2 = typeof(0.0)
  bar(outType1)
  bar(outType2)

prints:
ntyAlias
ntyFloat

this probably examples Example 1
I'd expect this to print:
ntyFloat
ntyFloat

  • Example 3
    these should be refactored (in semmagic.nim, semtypes.nim):
proc semTypeof2(c: PContext; n: PNode; prev: PType): PType =
  openScope(c)
  var m = BiggestInt 1 # typeOfIter
  if n.len == 3:
    let mode = semConstExpr(c, n[2])
    if mode.kind != nkIntLit:
      localError(c.config, n.info, "typeof: cannot evaluate 'mode' parameter at compile-time")
    else:
      m = mode.intVal
  let t = semExprWithType(c, n[1], if m == 1: {efInTypeof} else: {})
  closeScope(c)
  fixupTypeOf(c, prev, t)
  result = t.typ
proc semTypeOf(c: PContext; n: PNode): PNode =
  var m = BiggestInt 1 # typeOfIter
  if n.len == 3:
    let mode = semConstExpr(c, n[2])
    if mode.kind != nkIntLit:
      localError(c.config, n.info, "typeof: cannot evaluate 'mode' parameter at compile-time")
    else:
      m = mode.intVal
  result = newNodeI(nkTypeOfExpr, n.info)
  let typExpr = semExprWithType(c, n[1], if m == 1: {efInTypeof} else: {})
  result.add typExpr
  result.typ = makeTypeDesc(c, typExpr.typ)
  • Example 4
    => document typeof #16965
    the docs for typeof are bad; there should at least be a runnableExamples:
    image

  • Example 5
    this seems un-necessary in semMagic (and seems to pass test suite without it) which is a hot-spot and bypasses normal overload resolution:

  of mTypeOf:
    markUsed(c, n.info, s)
    result = semTypeOf(c, n)

(the other block like this in magicsAfterOverloadResolution seems correct though)

Additional Information

@metagn
Copy link
Collaborator

metagn commented Feb 11, 2021

both should behave the same, and both should print float64:
float64
float64

Is outType not supposed to be an alias in both of these situations? Is there supposed to be a special rule where if the RHS is an expression that resolves to a type, no alias is created?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants