Open BigBoyBarney opened 6 days ago
Looks like the cause for this error is that the downcased StringLiteral
s have no location.
Adding at(name_loc)
to the result value of StringLiteral#downcase
fixes that. But it's also obvious that similar methods have exactly the same issue.
All #interpret
implementations receive the name_loc
parameter. But none of them uses it to attach a location to the result value. And only one single method (TypeDef#name
) attaches a location at all.
Macro methods which return "fresh" values should have the location of the call that created them. Like in this example, the downased string should have it's location point to the call to StringLiteral#downcase
.
Ones that act as getters, returning already existing values (may be duplicates, though), the location should be that of the original value (like in TypeDef#name
).
The former is probably much more common, so we could consider adding a catch-all in Interpreter#visit(call)
which assigns the call's location to the return value. Existing locations should not be overridden, of course. So implementations of #interpret
could still assign a different location. We'll need to go through the methods to check which ones need that.
But the catch-all would remove the need to add .at(name_loc)
to most of the implementations.
I suppose it's not really feasible to assume that every AST node will always have an associated location. Ideally that would be the case, but unless there's a hard rule to enforce that, it cannot be expected at 100%.
Under that premise the compiler (or LLVM) should probably be able to gracefully handle missing location information. This affects only AST nodes that can lead to function calls, which is the case for ArrayLiteral
like in the OP.
The compiler must not crash on that. I'd even say that missing debug location should not prevent compilation. It does not seem to be really relevant to the integrity of the program that everything has a location. 🤷 I'm not sure about the reasoning why this is an error in LLVM.
Here are a couple of ideas:
Unrelated to that, we should add location information to more AST nodes. I'll implement the suggestion from https://github.com/crystal-lang/crystal/issues/15202#issuecomment-2482924310.
Here is the relevant code in LLVM: https://github.com/llvm/llvm-project/blob/aa746495affb3ab94daafcbe09bfb229dd27429f/llvm/lib/IR/Verifier.cpp#L3787-L3799
I've submitted a fix for the immediate problem with the code in the OP: #15213 This does not affect the deeper issue that we can potentially pass calls with missing location data to LLVM.
Hi!
The following minimal example compiles and runs with
crystal run
, but results in a compiler error withcrystal run --release
The error
Removing
.map &.downcase
compiles correctly with--release
. Passing--no-debug
fixes it as well, obviously :rofl:Relevant information:
Crystal 1.14.0 [dacd97b] (2024-10-09)
LLVM: 19.1.0 Default target: x86_64-redhat-linux-gnu
Fedora Linux 41
Edit:
Interestingly, if I'm not adding the elements to an array, it works.