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

[BUG] Type deduction deduces a pointer from an int #1347

Open
threeifbyair opened this issue Dec 12, 2024 · 4 comments
Open

[BUG] Type deduction deduces a pointer from an int #1347

threeifbyair opened this issue Dec 12, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@threeifbyair
Copy link

When creating a variable using >> or & on an integral type, somehow the deduced type is some kind of pointer rather than an integer.

https://cpp2.godbolt.org/z/sf4Y7MxMz

bar: (foo: uint32_t) -> uint32_t = {
    x := foo >> 16;
    y := foo & 0xFFFF;
    return (x+1) ^ (y-1);
}

gives

example.cpp2(4,22): error: - - pointer arithmetic is illegal - use std::span or gsl::span instead
  ==> program violates bounds safety guarantee - see previous errors

If I explicitly say that y is a uint32_t, everything is fine, but why does cppfront think that it's a pointer?
(I've seen the same issue with x, but it doesn't reproduce in this example.)

@threeifbyair threeifbyair added the bug Something isn't working label Dec 12, 2024
@gregmarr
Copy link
Contributor

Very weird. This works:

z := x + y; 
return (x+1) ^ (z-1);

Something is definitely off in the bounds safety check.

@zaucy
Copy link

zaucy commented Dec 13, 2024

I believe its because foo & is being interpreted as foo address by mistake? Adding parenthesis seems to work as well

bar: (foo: uint32_t) -> uint32_t = {
    x := foo >> 16;
    y := (foo) & 0xFFFF;
    return (x+1) ^ (y-1);
}

@gregmarr
Copy link
Contributor

Yes, but this means that it's going back to the line where y is declared to determine whether it's a pointer or not, and not parsing it properly, which seems odd to me.

@dhthwy
Copy link

dhthwy commented Dec 13, 2024

Yeah, it seems to be parsing as

(foo&) 0xFFFF

where the 0xFFFF is handled as if doesn't exist.

which should be an ill formed expression (and it is with the parens there)

But what about

y := foo && 0xFFF;

Is that a logical and or take the address of foo and bit and?

Seems to be some ambiguity to flesh out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants