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

Misleading error message when mutable variable shadows outer function #641

Open
jiribenes opened this issue Oct 14, 2024 · 0 comments
Open

Comments

@jiribenes
Copy link
Contributor

jiribenes commented Oct 14, 2024

The following code has a somewhat misleading error message: there is a sum which is mutable, it's just not something we can reach:
Screenshot 2024-10-14 at 15 01 15

The code in question
def sum(xs: List[Int]): Int = {
  var sum = xs.foreach { x =>
    sum = sum + x
    //  ^ ~~~
  }
  sum
}

Of course when you use res for the mutable variable and keep sum for the function, you get Could not resolve term res, which is probably a better message in this case.

I have no real use case for this, I just introduced a typo by accident in https://effekt-lang.org/tour/variables and then wondered for a brief moment.


Here's the relevant compiler code:

case source.Assign(id, expr) => Context.resolveVar(id) match {
case _: VarBinder | _: RegBinder => resolveGeneric(expr)
case _: ValBinder | _: ValueParam => Context.abort(pretty"Can only assign to mutable variables, but ${id.name} is a constant.")
case y: Wildcard => Context.abort(s"Trying to assign to a wildcard, which is not allowed.")
case _ => Context.abort(s"Can only assign to mutable variables.")
}

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

1 participant