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

for i in range(10) crashes the compiler #605

Closed
exhu opened this issue Sep 27, 2013 · 7 comments
Closed

for i in range(10) crashes the compiler #605

exhu opened this issue Sep 27, 2013 · 7 comments
Assignees

Comments

@exhu
Copy link
Contributor

exhu commented Sep 27, 2013

This code causes SEGFAULT on compiler:

for i in range(10):
    echo(i)
@Varriount
Copy link
Contributor

Here's a gist with more information on the segfault. The compiler was built from the latest revision, on windows 64 as a 64-bit executable, in debug mode (for the tracebacks)

https://gist.github.com/Varriount/6862082

@cablehead
Copy link

This crashes on OSX as well.

@cablehead
Copy link

While this shouldn't segfault, range is not a proc, but a type generator: http://nimrod-code.org/system.html#148

So the code snippet doesn't really make sense.

I believe this would be idiomatic:

for i in 0 .. <10: echo(i)

@mflamer
Copy link
Contributor

mflamer commented Oct 8, 2013

OK, that's what I thought I was not sure if there was also a proc named "range" that I wasn't aware of.

@mflamer
Copy link
Contributor

mflamer commented Oct 22, 2013

There is something funny going on here... kinds=211106232576000? It gets called from

case skipTypes(v.typ, abstractInst-{tyTypeDesc}).kind at semtypes.nim:49

I'm not sure what abstractInst is here.

#0  @skiptypes_181120@12 (t=<optimized out>, kinds=211106232576000)
    at c:/users/mflamer/dropbox/dev/nimrod/nimrod/compiler/types.nim:153
        result = 0x1348130
#1  0x0047c159 in @semenum_295441@12 (c=<optimized out>, n=<optimized out>, 
    prev=0x13480d8)
    at c:/users/mflamer/dropbox/dev/nimrod/nimrod/compiler/semtypes.nim:49
        v = 0x134a370
        strval = 0x0
        LOC20 = 0x0
        LOC65 = <optimized out>
        result = <optimized out>
        counter = <optimized out>
        x = <optimized out>
        e = <optimized out>
        base = <optimized out>
        ispure = <optimized out>
        LOC15 = <optimized out>
        hasnull = <optimized out>
        i_295544 = 1
        HEX3Atmp_295800 = <optimized out>
        res_295802 = 1

@Varriount
Copy link
Contributor

Ok, the last three function calls in the stack trace are where the damage is being done, I think.
A very similar error to the one above can be triggered by var x = range(10) , and the only functions calls they share are the last three.

@ghost ghost assigned zah Jan 23, 2014
@zah
Copy link
Member

zah commented Feb 17, 2014

This now produces a slightly confusing, but still valid error message:

Error: conversion from int literal(10) to range is invalid

@zah zah closed this as completed Feb 17, 2014
Clyybber pushed a commit to Clyybber/Nim that referenced this issue Sep 16, 2023
605: template non-ast param limited to lookup substitution r=zerbina a=saem

## Summary

* non-ast params (excluding `typed` and `untyped`) are only substituted
  for usage positions
* where a usage positionis is any syntactic position where a symbol is
  not intended to be introduced
* in additition, the manual and semtempl module have some doc comments
  outlining the new direction for templates

## Details

Starting with a motivating example, this now works after the change:

```nim
template foo(data: int): untyped =
  proc bar(data: int): int =
    2 * data
  bar(data)
doAssert foo(2) == 4
```

The reason this works now is `data` the template arg is a non-ast type,
it's not substituted into `data` the proc param, and `data` the proc
param is instead converted into a symbol. This means that when `data`
the proc body usage is looked up, it's lexically resolved with `data`
the proc param. Finally, when `data` the call argument's usage is looked
up, the template param is found and substituted.

### Further Details

Template parameters that are neither `typed` or `untyped` will no
longer subsititute for definitional syntax positions, or syntax
positions that meant to introduce routines, variables, types and other
named definitions. For non-ast typed substitution lexical scope based
lookups are used. AST typed template parameter substitutions continue to
work as is, since they're arbitrary symbolic substitutions.

This makes more sense given that we're substituting syntactic symbol
usage as opposed to syntactic symbol introductions; but it also removes
a vector by which symbol pollution can take place.

As templates have lots of rough edges in implementation there are still
many bugs, but these are almost entirely pre-existing.

### Overview of Concepts Introduced:

This is a brief overview, see the module and docs for more info, also
this is developing, these are still mostly in docs:
* the concept of template substitution being policy being set by
  template output type has been introduced
* a template body is to be thought of as an out parameter with the same
  type as the template output
* additionally, a template body can be considered as a form of quasi
  quoting, where the type determines how we treat the substitution of
  various kinds of parameters (ast and non-ast types).



Co-authored-by: saem <saemghani+github@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants