-
Notifications
You must be signed in to change notification settings - Fork 13.4k
fn ptr is structural match #64431
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
Merged
bors
merged 2 commits into
rust-lang:master
from
pnkfelix:issue-63479-fnptr-is-structural-match
Sep 14, 2019
Merged
fn ptr is structural match #64431
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/test/ui/rfc1445/fn-ptr-is-structurally-matchable.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// run-pass | ||
|
||
// This file checks that fn ptrs are considered structurally matchable. | ||
// See also rust-lang/rust#63479. | ||
|
||
fn main() { | ||
let mut count = 0; | ||
|
||
// A type which is not structurally matchable: | ||
struct NotSM; | ||
|
||
// And one that is: | ||
#[derive(PartialEq, Eq)] | ||
struct SM; | ||
|
||
fn trivial() {} | ||
|
||
fn sm_to(_: SM) {} | ||
fn not_sm_to(_: NotSM) {} | ||
fn to_sm() -> SM { SM } | ||
fn to_not_sm() -> NotSM { NotSM } | ||
|
||
// To recreate the scenario of interest in #63479, we need to add | ||
// a ref-level-of-indirection so that we descend into the type. | ||
|
||
fn r_sm_to(_: &SM) {} | ||
fn r_not_sm_to(_: &NotSM) {} | ||
fn r_to_r_sm(_: &()) -> &SM { &SM } | ||
fn r_to_r_not_sm(_: &()) -> &NotSM { &NotSM } | ||
|
||
#[derive(PartialEq, Eq)] | ||
struct Wrap<T>(T); | ||
|
||
// In the code below, we put the match input into a local so that | ||
// we can assign it an explicit type that is an fn ptr instead of | ||
// a singleton type of the fn itself that the type inference would | ||
pnkfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// otherwise assign. | ||
|
||
// Check that fn() is #[structural_match] | ||
pnkfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const CFN1: Wrap<fn()> = Wrap(trivial); | ||
let input: Wrap<fn()> = Wrap(trivial); | ||
match Wrap(input) { | ||
Wrap(CFN1) => count += 1, | ||
Wrap(_) => {} | ||
}; | ||
|
||
// Check that fn(T) is #[structural_match] when T is too. | ||
pnkfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const CFN2: Wrap<fn(SM)> = Wrap(sm_to); | ||
let input: Wrap<fn(SM)> = Wrap(sm_to); | ||
match Wrap(input) { | ||
Wrap(CFN2) => count += 1, | ||
Wrap(_) => {} | ||
}; | ||
|
||
// Check that fn() -> T is #[structural_match] when T is too. | ||
pnkfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const CFN3: Wrap<fn() -> SM> = Wrap(to_sm); | ||
let input: Wrap<fn() -> SM> = Wrap(to_sm); | ||
match Wrap(input) { | ||
Wrap(CFN3) => count += 1, | ||
Wrap(_) => {} | ||
}; | ||
|
||
// Check that fn(T) is #[structural_match] even if T is not. | ||
pnkfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const CFN4: Wrap<fn(NotSM)> = Wrap(not_sm_to); | ||
let input: Wrap<fn(NotSM)> = Wrap(not_sm_to); | ||
match Wrap(input) { | ||
Wrap(CFN4) => count += 1, | ||
Wrap(_) => {} | ||
}; | ||
|
||
// Check that fn() -> T is #[structural_match] even if T is not. | ||
pnkfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const CFN5: Wrap<fn() -> NotSM> = Wrap(to_not_sm); | ||
let input: Wrap<fn() -> NotSM> = Wrap(to_not_sm); | ||
match Wrap(input) { | ||
Wrap(CFN5) => count += 1, | ||
Wrap(_) => {} | ||
}; | ||
|
||
// Check that fn(&T) is #[structural_match] when T is too. | ||
pnkfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const CFN6: Wrap<fn(&SM)> = Wrap(r_sm_to); | ||
let input: Wrap<fn(&SM)> = Wrap(r_sm_to); | ||
match Wrap(input) { | ||
Wrap(CFN6) => count += 1, | ||
Wrap(_) => {} | ||
}; | ||
|
||
// Check that fn() -> &T is #[structural_match] when T is too. | ||
pnkfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const CFN7: Wrap<fn(&()) -> &SM> = Wrap(r_to_r_sm); | ||
let input: Wrap<fn(&()) -> &SM> = Wrap(r_to_r_sm); | ||
match Wrap(input) { | ||
Wrap(CFN7) => count += 1, | ||
Wrap(_) => {} | ||
}; | ||
|
||
// Check that fn(T) is #[structural_match] even if T is not. | ||
pnkfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const CFN8: Wrap<fn(&NotSM)> = Wrap(r_not_sm_to); | ||
let input: Wrap<fn(&NotSM)> = Wrap(r_not_sm_to); | ||
match Wrap(input) { | ||
Wrap(CFN8) => count += 1, | ||
Wrap(_) => {} | ||
}; | ||
|
||
// Check that fn() -> T is #[structural_match] even if T is not. | ||
pnkfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const CFN9: Wrap<fn(&()) -> &NotSM> = Wrap(r_to_r_not_sm); | ||
let input: Wrap<fn(&()) -> &NotSM> = Wrap(r_to_r_not_sm); | ||
match Wrap(input) { | ||
Wrap(CFN9) => count += 1, | ||
Wrap(_) => {} | ||
}; | ||
|
||
// Check that a type which has fn ptrs is `#[structural_match]`. | ||
pnkfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[derive(PartialEq, Eq)] | ||
struct Foo { | ||
alpha: fn(NotSM), | ||
beta: fn() -> NotSM, | ||
gamma: fn(SM), | ||
delta: fn() -> SM, | ||
} | ||
|
||
const CFOO: Foo = Foo { | ||
alpha: not_sm_to, | ||
beta: to_not_sm, | ||
gamma: sm_to, | ||
delta: to_sm, | ||
}; | ||
|
||
let input = Foo { alpha: not_sm_to, beta: to_not_sm, gamma: sm_to, delta: to_sm }; | ||
match input { | ||
CFOO => count += 1, | ||
Foo { .. } => {} | ||
}; | ||
|
||
// Final count must be 10 now if all | ||
pnkfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_eq!(count, 10); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// run-pass | ||
|
||
// The actual regression test from #63479. (Including this because my | ||
// first draft at fn-ptr-is-structurally-matchable.rs failed to actually | ||
// cover the case this hit; I've since expanded it accordingly, but the | ||
// experience left me wary of leaving this regression test out.) | ||
|
||
#[derive(Eq)] | ||
struct A { | ||
a: i64 | ||
} | ||
|
||
impl PartialEq for A { | ||
#[inline] | ||
fn eq(&self, other: &Self) -> bool { | ||
self.a.eq(&other.a) | ||
} | ||
} | ||
|
||
type Fn = fn(&[A]); | ||
|
||
fn my_fn(_args: &[A]) { | ||
println!("hello world"); | ||
} | ||
|
||
const TEST: Fn = my_fn; | ||
|
||
struct B(Fn); | ||
|
||
fn main() { | ||
let s = B(my_fn); | ||
match s { | ||
B(TEST) => println!("matched"), | ||
_ => panic!("didn't match") | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.