Skip to content

fix panic on ellipsis in pattern #4012

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
merged 1 commit into from
Apr 17, 2020
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
13 changes: 10 additions & 3 deletions crates/ra_hir_def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ impl ExprCollector<'_> {
ast::Pat::SlicePat(p) => {
let SlicePatComponents { prefix, slice, suffix } = p.components();

// FIXME properly handle `DotDotPat`
Pat::Slice {
prefix: prefix.into_iter().map(|p| self.collect_pat(p)).collect(),
slice: slice.map(|p| self.collect_pat(p)),
Expand All @@ -666,9 +667,15 @@ impl ExprCollector<'_> {
Pat::Missing
}
}
ast::Pat::DotDotPat(_) => unreachable!(
"`DotDotPat` requires special handling and should not be mapped to a Pat."
),
ast::Pat::DotDotPat(_) => {
// `DotDotPat` requires special handling and should not be mapped
// to a Pat. Here we are using `Pat::Missing` as a fallback for
// when `DotDotPat` is mapped to `Pat`, which can easily happen
// when the source code being analyzed has a malformed pattern
// which includes `..` in a place where it isn't valid.

Pat::Missing
}
// FIXME: implement
ast::Pat::BoxPat(_) | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing,
};
Expand Down
49 changes: 49 additions & 0 deletions crates/ra_hir_ty/src/tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,52 @@ fn main() {

assert_eq!("()", super::type_at_pos(&db, pos));
}

#[test]
fn issue_3999_slice() {
assert_snapshot!(
infer(r#"
fn foo(params: &[usize]) {
match params {
[ps @ .., _] => {}
}
}
"#),
@r###"
[8; 14) 'params': &[usize]
[26; 81) '{ ... } }': ()
[32; 79) 'match ... }': ()
[38; 44) 'params': &[usize]
[55; 67) '[ps @ .., _]': [usize]
[65; 66) '_': usize
[71; 73) '{}': ()
"###
);
}

#[test]
fn issue_3999_struct() {
// rust-analyzer should not panic on seeing this malformed
// record pattern.
assert_snapshot!(
infer(r#"
struct Bar {
a: bool,
}
fn foo(b: Bar) {
match b {
Bar { a: .. } => {},
}
}
"#),
@r###"
[36; 37) 'b': Bar
[44; 96) '{ ... } }': ()
[50; 94) 'match ... }': ()
[56; 57) 'b': Bar
[68; 81) 'Bar { a: .. }': Bar
[77; 79) '..': bool
[85; 87) '{}': ()
"###
);
}