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

Typecheck patterns of all match arms first, so we get types for bindings #55819

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,9 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
let discrim_diverges = self.diverges.get();
self.diverges.set(Diverges::Maybe);

// Typecheck the patterns first, so that we get types for all the
// bindings.
let all_arm_pats_diverge = arms.iter().map(|arm| {
// rust-lang/rust#55810: Typecheck patterns first (via eager
// collection into `Vec`), so we get types for all bindings.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, had I seen this PR, I would've suggested adding a note that the map below has side-effects in terms of type inference (unifying the types of all the arms together), and that's why it has to fully complete first.

let all_arm_pats_diverge: Vec<_> = arms.iter().map(|arm| {
let mut all_pats_diverge = Diverges::WarnedAlways;
for p in &arm.pats {
self.diverges.set(Diverges::Maybe);
Expand All @@ -644,7 +644,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
Diverges::Maybe => Diverges::Maybe,
Diverges::Always | Diverges::WarnedAlways => Diverges::WarnedAlways,
}
});
}).collect();

// Now typecheck the blocks.
//
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// compile-pass

// rust-lang/rust#55810: types for a binding in a match arm can be
// inferred from arms that come later in the match.

struct S;

impl S {
fn method(&self) -> bool {
unimplemented!()
}
}

fn get<T>() -> T {
unimplemented!()
}

fn main() {
match get() {
x if x.method() => {}
&S => {}
}
}