-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Emit diagnostic when using const
storing Fn
as pattern
#41434
Conversation
```rust fn f1() { println!("1"); } fn f2() { println!("2"); } struct S(fn()); const CONST: S = S(f1); fn main() { let x = S(f2); match x { CONST => { println!("match"); } _ => {} } } ``` ```rust error: pattern contains `Fn`, use pattern guard instead --> <anon>.rs:14:9 | 13 | CONST => { | ^^^^^ pattern contains `Fn` | help: use a pattern guard instead: | x if x == CONST => { ``` Fix ICE.
r? @eddyb (rust_highfive has picked a reviewer for you, use r? to override) |
The error message seems vague and confusing to me. What's the reason why a function can't be in a match pattern? |
@durka the current compiler output is an ICE. Only reason I see to disallow it is not to change behaviour. |
I"m not sure if this check is in quite the right spot. I think I would expect it to be in the |
I guess I don't understand why the example program at the top is an unreasonable program. What's wrong with checking which fn I have in a variable? As a user, with this PR I get:
Yes it's better than an ICE, but this is not a good situation. |
This should be using lowercase-f |
EDIT2: Nevermind, an explicit |
i.e. just before evaluating an expression we should check for a reification adjustment on that expression and |
Why don't we want to support reifying fn pointers in const-eval? With MIRI, we'll have to, and still face this problem. |
@arielb1 It really comes down to whether we want to allow it in patterns - if we don't, we should ban it through "structural match", if we want to fix it so it works it's relatively straight-forward. |
Where structural_match = altering |
I mean that constants of types that contain function pointers can't be matched on - same thing with floats. |
Hey @estebank thanks again for the PR! It sounds like there's a few changes requested, though, do you think you'll have a chance to look into them? |
@alexcrichton I will, but probably not before the weekend. Would you prefer to keep this PR open for the comment history or creating a clean PR when I get back to this? |
Oh yeah it's fine to keep open, just doing some PR triaging this morning :) |
Ah ok coming back around to this, I'm gonna close this for now to help clear out the queue, but @estebank please feel free to resubmit! |
@alexcrichton I spent the last weekend looking into this, and hit a wall as the amount of code needed for getting it to work appropriately was getting bigger and bigger. I'll probably get back to this sometime in the future, but I can help if somebody else wants to take it before then. |
Fix #40657.