# Compiler error?

**URL:** <https://forum.crystal-lang.org/t/compiler-error/7415>\
**Category:** Help & Support\
**Created:** [November 16, 2024, 4:22pm UTC](https://forum.crystal-lang.org/t/compiler-error/7415 "2024-11-16T16:22:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [November 16, 2024, 4:22pm UTC](https://forum.crystal-lang.org/t/compiler-error/7415/1 "2024-11-16T16:22:36Z")

</div>

This compiles fine.

```auto
Crystal 1.14.0 [dacd97bcc] (2024-10-09)

LLVM: 18.1.6
Default target: x86_64-unknown-linux-gnu

```

```auto
def mpgen(p, steps = 1u64)
  puts "Starting Mp gen exponent = #{p}"
  puts "Starting Mp gen process at step #{steps}"
  r = (1.to_big_i << p) - 1
  modpnk = modpn = 3.to_big_i << p
  (steps-1).times { modpnk = (modpnk << 2) + modpn }
  primes_checked = 0u64
  loop do
    mp = modpnk + r
    print "\rsteps = #{steps}"
    p = mp.bit_length.to_u64
    if prime?(p)
      primes_checked += 1
      break if mp_prime?(p, mp)
    end
    steps += 1
    modpnk = (modpnk << 2) + modpn
  end
  print "\rsteps = #{steps}\n"
  puts "Primes checked = #{primes_checked}"
  puts "Next Mp prime = #{p}"
  p
end

```

Then I changed the code to this:

```auto
def mpgen(n, steps = 1u64)
  puts "Starting Mp gen exponent = #{n}"
  puts "Starting Mp gen process at step #{steps}"
  primes_checked = 0u64
  loop do
    mp = (3.to_big_i << n) + (1.to_big_i << n) - 1
    print "\rsteps = #{steps}"
    p = mp.bit_length.to_u64
    if prime?(p)
      primes_checked += 1
      break if mp_prime?(p, mp)
    end
    steps += 1
    n += 2
  end
  print "\rsteps = #{steps}\n"
  puts "Primes checked = #{primes_checked}"
  puts "Next Mp prime = #{p}"
  p
end

```

And now get this error.

```auto
Showing last frame. Use --error-trace for full trace.

In MPgenerate1.cr:40:25

 40 | mp = (3.to_big_i << n) + (1.to_big_i << n) - 1
                          ^
Error: expected argument #1 to 'BigInt#<<' to be Int, not (Tuple() | UInt64)

Overloads are:
 - BigInt#<<(other : Int)
 - Int#<<(count : Int)

```

When I change the order of those 2 terms it still points to the first `n`.

This has to be a compiler error, right?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [November 16, 2024, 6:44pm UTC](https://forum.crystal-lang.org/t/compiler-error/7415/2 "2024-11-16T18:44:34Z")

</div>

Do you have a runnable example that reproduces this error?

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [November 16, 2024, 7:07pm UTC](https://forum.crystal-lang.org/t/compiler-error/7415/3 "2024-11-16T19:07:15Z")

</div>

I put a link to the gist file of all the code in the Issues on this.

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [November 16, 2024, 7:52pm UTC](https://forum.crystal-lang.org/t/compiler-error/7415/4 "2024-11-16T19:52:59Z")

</div>

The compiler is technically right here. There’s a few things going on, but I don’t really think this is a compiler bug at least. Adding method parameter and return type restrictions makes the problem more clear.

1. Using `loop do` creates its own scope within the block, so the compiler has to handle the case where it doesn’t execute I guess, using `while true` does not suffer from this and works just fine.
2. Your variable `p` is basically conflicting with the `::p()` method which returns the arguments passed to it, and since its only assigned in the `loop do`, if that doesn’t execute the compiler will call `::p()` with no arguments so it returns an empty tuple, which without a type restriction on the method or `n` parameter, is allowed to pass thru, turning the type of `n` in to `Tuple() | UInt64`.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [November 16, 2024, 8:38pm UTC](https://forum.crystal-lang.org/t/compiler-error/7415/5 "2024-11-16T20:38:44Z")

</div>

I changed `lopp do` to `while true` and it did compile and gives correct results.

I don’t understand (yet) the other reasoning around the `p` variable, but I’ll play some more with it to see if I can get it to compile.

Even if it isn’t a formal compiler error, it’s certainly unexpected behavior, and maybe a better error message is warranted.

Thanks for the quick feedback. 🙂

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [November 17, 2024, 12:07am UTC](https://forum.crystal-lang.org/t/compiler-error/7415/6 "2024-11-17T00:07:30Z")

</div>

OK, I got it to work with `loop do`.

I initialized `p` before the loop and it works.  
But the error message isn’t very helpful because it points to `n`, which is not the cause of the error (it’s also defined before `loop do`), it’s apparently an artifact of it.

It’s these subtle differences like between things like `loop do` and `while true` that for people coming from dynamic languages like Ruby, et al, can be a PITA.

```auto
def mpgen(n, steps = 1u64)
  puts "Starting Mp gen exponent = #{n}"
  puts "Starting Mp gen process at step #{steps}"
  primes_checked = 0u64
  p = 0u64 # <---- need for it to compile
  loop do
    mp = (3.to_big_i << n) + (1.to_big_i << n) - 1
    p = mp.bit_length.to_u64
    print "\rsteps = #{steps}; checking prime #{p}"
    if prime?(p)
      primes_checked += 1
      break if mp_prime?(p, mp)
    end
    steps += 1
    n += 2
  end
  print "\rsteps = #{steps}\n"
  puts "Primes checked = #{primes_checked}"
  puts "Next Mp prime = #{p}"
  p
end

```

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [November 17, 2024, 3:10am UTC](https://forum.crystal-lang.org/t/compiler-error/7415/7 "2024-11-17T03:10:13Z")

</div>

> [@jzakiya](#):
>
> But the error message isn’t very helpful because it points to `n`, which is not the cause of the error (it’s also defined before `loop do`)

If `mpgen` is recursive (for example, if `mp_prime?` calls back into it) or if something else in your program is feeding the return value of one `mpgen` call to another, then pointing to `n` makes sense. The method [`p`](https://crystal-lang.org/api/1.14.0/toplevel.html#p%28%2Aobjects%29-class-method) with no arguments returns an empty `Tuple`.

```crystal
p typeof(p)
# => Tuple()

```

So if you’re doing something like this:

```crystal
n = 0
while n < some_value
  n = mpgen(n)
  # ...
end

```

Then the compiler will infer from the fact that you returned a `Tuple()` from an invocation of `mpgen` that `n` inside of `mpgen` can be a `Tuple()`.

```crystal
def mpgen(n)
  loop do
    p = n
    break
  end
  p
end

n = 0
3.times do
  puts n
  n = mpgen(n)
end
# => 0
# => {}
# => {}

```

> [@jzakiya](#):
>
> It’s these subtle differences like between things like `loop do` and `while true` that for people coming from dynamic languages like Ruby, et al, can be a PITA.

How so? The scoping rules between `loop do` and `while true` in Ruby are the same as in Crystal. This would’ve failed in Ruby, too, except the value would’ve been `nil` (`p` with no args returns `nil`).

```ruby
def preinitialized
  p = :preinitialized
  loop do
    p = 0
    break
  end
  p
end

def uninitialized
  loop do
    p = 0
    break
  end
  p
end

p preinitialized # => 0
p uninitialized # => nil

```

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [November 17, 2024, 5:50pm UTC](https://forum.crystal-lang.org/t/compiler-error/7415/8 "2024-11-17T17:50:25Z")

</div>

The issue for me was the error message was very confusing. As soon as @Blacksmoke16 said the word **scope** I knew exactly what the problem was, and fixed it.

If the error message had said something like:  
`p not in scope`  
or even  
`variable in loop not in scope`

I would have known exactly what the problem was.

These are the specific types of error messages the Rust compiler gives.

Hopefully I’ll be cognizant of these loop differences (again) and won’t repeat this mistake.

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [November 22, 2024, 9:24am UTC](https://forum.crystal-lang.org/t/compiler-error/7415/9 "2024-11-22T09:24:01Z")

</div>

> [@jzakiya](#):
>
> It’s these subtle differences like between things like `loop do` and `while true` that for people coming from dynamic languages like Ruby, et al, can be a PITA.

This is not the different of `loop do` with `while true`, it is the behavior of `block`.

block always create a new local scope, in fact, are you try it use Ruby? this behavior exactly same as Ruby.
