Skip to content

Commit 65f92a5

Browse files
committed
address review
1 parent 1c217b6 commit 65f92a5

File tree

8 files changed

+51
-102
lines changed

8 files changed

+51
-102
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14781478
let pat_ty = self.node_ty(decl.pat.hir_id);
14791479
self.overwrite_local_ty_if_err(decl.hir_id, decl.pat, pat_ty);
14801480

1481-
if let Some(blk) = decl.origin.try_get_els() {
1481+
if let Some(blk) = decl.origin.try_get_else() {
14821482
let previous_diverges = self.diverges.get();
14831483
let else_ty = self.check_block_with_expected(blk, NoExpectation);
14841484
let cause = self.cause(blk.span, ObligationCauseCode::LetElse);

compiler/rustc_hir_typeck/src/gather_locals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub(super) enum DeclOrigin<'a> {
2121
}
2222

2323
impl<'a> DeclOrigin<'a> {
24-
pub(super) fn try_get_els(&self) -> Option<&'a hir::Block<'a>> {
24+
pub(super) fn try_get_else(&self) -> Option<&'a hir::Block<'a>> {
2525
match self {
2626
Self::LocalDecl { els } => *els,
2727
Self::LetExpr => None,

compiler/rustc_hir_typeck/src/pat.rs

+31-86
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
163163
/// Conversely, inside this module, `check_pat_top` should never be used.
164164
#[instrument(level = "debug", skip(self, pat_info))]
165165
fn check_pat(&self, pat: &'tcx Pat<'tcx>, expected: Ty<'tcx>, pat_info: PatInfo<'tcx, '_>) {
166-
let PatInfo { binding_mode: def_bm, top_info: ti, decl_origin } = pat_info;
166+
let PatInfo { binding_mode: def_bm, top_info: ti, .. } = pat_info;
167167
let path_res = match &pat.kind {
168168
PatKind::Path(qpath) => {
169169
Some(self.resolve_ty_and_res_fully_qualified_call(qpath, pat.hir_id, pat.span))
@@ -172,76 +172,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
172172
};
173173
let adjust_mode = self.calc_adjust_mode(pat, path_res.map(|(res, ..)| res));
174174
let (expected, def_bm) = self.calc_default_binding_mode(pat, expected, def_bm, adjust_mode);
175+
let pat_info =
176+
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin: pat_info.decl_origin };
175177

176178
let ty = match pat.kind {
177179
PatKind::Wild => expected,
178180
PatKind::Lit(lt) => self.check_pat_lit(pat.span, lt, expected, ti),
179181
PatKind::Range(lhs, rhs, _) => self.check_pat_range(pat.span, lhs, rhs, expected, ti),
180-
PatKind::Binding(ba, var_id, _, sub) => self.check_pat_ident(
181-
pat,
182-
ba,
183-
var_id,
184-
sub,
185-
expected,
186-
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin },
187-
),
188-
PatKind::TupleStruct(ref qpath, subpats, ddpos) => self.check_pat_tuple_struct(
189-
pat,
190-
qpath,
191-
subpats,
192-
ddpos,
193-
expected,
194-
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin },
195-
),
182+
PatKind::Binding(ba, var_id, _, sub) => {
183+
self.check_pat_ident(pat, ba, var_id, sub, expected, pat_info)
184+
}
185+
PatKind::TupleStruct(ref qpath, subpats, ddpos) => {
186+
self.check_pat_tuple_struct(pat, qpath, subpats, ddpos, expected, pat_info)
187+
}
196188
PatKind::Path(ref qpath) => {
197189
self.check_pat_path(pat, qpath, path_res.unwrap(), expected, ti)
198190
}
199-
PatKind::Struct(ref qpath, fields, has_rest_pat) => self.check_pat_struct(
200-
pat,
201-
qpath,
202-
fields,
203-
has_rest_pat,
204-
expected,
205-
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin },
206-
),
191+
PatKind::Struct(ref qpath, fields, has_rest_pat) => {
192+
self.check_pat_struct(pat, qpath, fields, has_rest_pat, expected, pat_info)
193+
}
207194
PatKind::Or(pats) => {
208195
for pat in pats {
209-
self.check_pat(
210-
pat,
211-
expected,
212-
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin },
213-
);
196+
self.check_pat(pat, expected, pat_info);
214197
}
215198
expected
216199
}
217-
PatKind::Tuple(elements, ddpos) => self.check_pat_tuple(
218-
pat.span,
219-
elements,
220-
ddpos,
221-
expected,
222-
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin },
223-
),
224-
PatKind::Box(inner) => self.check_pat_box(
225-
pat.span,
226-
inner,
227-
expected,
228-
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin },
229-
),
230-
PatKind::Ref(inner, mutbl) => self.check_pat_ref(
231-
pat,
232-
inner,
233-
mutbl,
234-
expected,
235-
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin },
236-
),
237-
PatKind::Slice(before, slice, after) => self.check_pat_slice(
238-
pat.span,
239-
before,
240-
slice,
241-
after,
242-
expected,
243-
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin },
244-
),
200+
PatKind::Tuple(elements, ddpos) => {
201+
self.check_pat_tuple(pat.span, elements, ddpos, expected, pat_info)
202+
}
203+
PatKind::Box(inner) => self.check_pat_box(pat.span, inner, expected, pat_info),
204+
PatKind::Ref(inner, mutbl) => self.check_pat_ref(pat, inner, mutbl, expected, pat_info),
205+
PatKind::Slice(before, slice, after) => {
206+
self.check_pat_slice(pat.span, before, slice, after, expected, pat_info)
207+
}
245208
};
246209

247210
self.write_ty(pat.hir_id, ty);
@@ -624,7 +587,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
624587
expected: Ty<'tcx>,
625588
pat_info: PatInfo<'tcx, '_>,
626589
) -> Ty<'tcx> {
627-
let PatInfo { binding_mode: def_bm, top_info: ti, decl_origin } = pat_info;
590+
let PatInfo { binding_mode: def_bm, top_info: ti, .. } = pat_info;
628591

629592
// Determine the binding mode...
630593
let bm = match ba {
@@ -663,11 +626,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
663626
}
664627

665628
if let Some(p) = sub {
666-
self.check_pat(
667-
p,
668-
expected,
669-
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin },
670-
);
629+
self.check_pat(p, expected, pat_info);
671630
}
672631

673632
local_ty
@@ -892,37 +851,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
892851
expected: Ty<'tcx>,
893852
pat_info: PatInfo<'tcx, '_>,
894853
) -> Ty<'tcx> {
895-
let PatInfo { binding_mode: def_bm, top_info: ti, decl_origin } = pat_info;
896-
897854
// Resolve the path and check the definition for errors.
898855
let (variant, pat_ty) = match self.check_struct_path(qpath, pat.hir_id) {
899856
Ok(data) => data,
900857
Err(guar) => {
901858
let err = Ty::new_error(self.tcx, guar);
902859
for field in fields {
903-
let ti = ti;
904-
self.check_pat(
905-
field.pat,
906-
err,
907-
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin },
908-
);
860+
self.check_pat(field.pat, err, pat_info);
909861
}
910862
return err;
911863
}
912864
};
913865

914866
// Type-check the path.
915-
self.demand_eqtype_pat(pat.span, expected, pat_ty, ti);
867+
self.demand_eqtype_pat(pat.span, expected, pat_ty, pat_info.top_info);
916868

917869
// Type-check subpatterns.
918-
if self.check_struct_pat_fields(
919-
pat_ty,
920-
&pat,
921-
variant,
922-
fields,
923-
has_rest_pat,
924-
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin },
925-
) {
870+
if self.check_struct_pat_fields(pat_ty, &pat, variant, fields, has_rest_pat, pat_info) {
926871
pat_ty
927872
} else {
928873
Ty::new_misc_error(self.tcx)
@@ -2144,11 +2089,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21442089
/// If we're in an irrefutable pattern we prefer the array impl candidate given that
21452090
/// the slice impl candidate would be be rejected anyway (if no ambiguity existed).
21462091
fn pat_is_irrefutable(&self, decl_origin: Option<DeclOrigin<'_>>) -> bool {
2147-
if let Some(decl_origin) = decl_origin {
2148-
decl_origin.try_get_els().is_none()
2149-
&& matches!(decl_origin, DeclOrigin::LocalDecl { .. })
2150-
} else {
2151-
false
2092+
match decl_origin {
2093+
Some(DeclOrigin::LocalDecl { els: None }) => true,
2094+
Some(DeclOrigin::LocalDecl { els: Some(_) } | DeclOrigin::LetExpr) | None => false,
21522095
}
21532096
}
21542097

@@ -2183,6 +2126,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21832126
}
21842127

21852128
let expected = self.structurally_resolve_type(span, expected);
2129+
debug!(?expected);
2130+
21862131
let (element_ty, opt_slice_ty, inferred) = match *expected.kind() {
21872132
// An array, so we might have something like `let [a, b, c] = [0, 1, 2];`.
21882133
ty::Array(element_ty, len) => {

tests/ui/array-slice-vec/slice-pat-type-mismatches.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ fn main() {
3030
}
3131

3232
fn another_fn_to_avoid_suppression() {
33-
match Default
34-
//~^ ERROR expected value, found trait
35-
{
33+
match Default::default() {
3634
[] => {}
35+
//~^ ERROR type annotations needed
3736
};
3837
}

tests/ui/array-slice-vec/slice-pat-type-mismatches.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ error[E0425]: cannot find value `does_not_exist` in this scope
44
LL | match does_not_exist {
55
| ^^^^^^^^^^^^^^ not found in this scope
66

7-
error[E0423]: expected value, found trait `Default`
8-
--> $DIR/slice-pat-type-mismatches.rs:33:11
9-
|
10-
LL | match Default
11-
| ^^^^^^^ not a value
12-
137
error[E0529]: expected an array or slice, found `String`
148
--> $DIR/slice-pat-type-mismatches.rs:3:9
159
|
@@ -28,7 +22,13 @@ error[E0528]: pattern requires at least 4 elements but array has 3
2822
LL | [0, 1, 2, 3, x @ ..] => {}
2923
| ^^^^^^^^^^^^^^^^^^^^ pattern cannot match array of 3 elements
3024

25+
error[E0282]: type annotations needed
26+
--> $DIR/slice-pat-type-mismatches.rs:34:9
27+
|
28+
LL | [] => {}
29+
| ^^ cannot infer type
30+
3131
error: aborting due to 5 previous errors
3232

33-
Some errors have detailed explanations: E0423, E0425, E0527, E0528, E0529.
34-
For more information about an error, try `rustc --explain E0423`.
33+
Some errors have detailed explanations: E0282, E0425, E0527, E0528, E0529.
34+
For more information about an error, try `rustc --explain E0282`.

tests/ui/pattern/issue-76342.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// check-pass
2+
3+
// Test that we infer the expected type of a pattern to an array of the given length.
4+
25
#![allow(unused_variables)]
36
struct Zeroes;
47
impl Into<[usize; 2]> for Zeroes {

tests/ui/pattern/slice-pattern-refutable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Test that we do not infer the expected types of patterns to an array
2+
// if we're in a refutable pattern.
13
#![allow(unused_variables)]
24

35
struct Zeroes;

tests/ui/pattern/slice-pattern-refutable.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/slice-pattern-refutable.rs:12:9
2+
--> $DIR/slice-pattern-refutable.rs:14:9
33
|
44
LL | let [a, b, c] = Zeroes.into() else {
55
| ^^^^^^^^^
@@ -10,7 +10,7 @@ LL | let [a, b, c]: /* Type */ = Zeroes.into() else {
1010
| ++++++++++++
1111

1212
error[E0282]: type annotations needed
13-
--> $DIR/slice-pattern-refutable.rs:19:31
13+
--> $DIR/slice-pattern-refutable.rs:21:31
1414
|
1515
LL | if let [a, b, c] = Zeroes.into() {
1616
| --------- ^^^^
@@ -23,7 +23,7 @@ LL | if let [a, b, c] = <Zeroes as Into<T>>::into(Zeroes) {
2323
| ++++++++++++++++++++++++++ ~
2424

2525
error[E0282]: type annotations needed
26-
--> $DIR/slice-pattern-refutable.rs:26:31
26+
--> $DIR/slice-pattern-refutable.rs:28:31
2727
|
2828
LL | if let [a, b, c] = Zeroes.into() {
2929
| --------- ^^^^

0 commit comments

Comments
 (0)