Skip to content

Commit 9f8cd9d

Browse files
committed
Auto merge of #61825 - Centril:tauv-infer-fix, r=petrochenkov
type_alias_enum_variants: fix #61801; allow a path pattern to infer Fix #61801. Given a type-relative path pattern referring to an enum variant through a type alias, allow inferring the generic argument applied in the expectation set by the scrutinee of a `match` expression. Similar issues may exist for `let` statements but I don't know how to test for that since `PhantomData<T>` is necessary...) The gist of the problem here was that `resolve_ty_and_res_ufcs` was called twice which is apparently no good... It is possible that this PR is papering over some deeper problem, but that is beyond my knowledge of the compiler. r? @petrochenkov cc @eddyb @alexreg cc #61682 cc #49683
2 parents 9606f6f + 065151f commit 9f8cd9d

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/librustc_typeck/check/_match.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5050

5151
debug!("check_pat_walk(pat={:?},expected={:?},def_bm={:?})", pat, expected, def_bm);
5252

53+
let mut path_resolution = None;
5354
let is_non_ref_pat = match pat.node {
5455
PatKind::Struct(..) |
5556
PatKind::TupleStruct(..) |
@@ -65,8 +66,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6566
}
6667
}
6768
PatKind::Path(ref qpath) => {
68-
let (def, _, _) = self.resolve_ty_and_res_ufcs(qpath, pat.hir_id, pat.span);
69-
match def {
69+
let resolution = self.resolve_ty_and_res_ufcs(qpath, pat.hir_id, pat.span);
70+
path_resolution = Some(resolution);
71+
match resolution.0 {
7072
Res::Def(DefKind::Const, _) | Res::Def(DefKind::AssocConst, _) => false,
7173
_ => true,
7274
}
@@ -294,7 +296,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
294296
)
295297
}
296298
PatKind::Path(ref qpath) => {
297-
self.check_pat_path(pat, qpath, expected)
299+
self.check_pat_path(pat, path_resolution.unwrap(), qpath, expected)
298300
}
299301
PatKind::Struct(ref qpath, ref fields, etc) => {
300302
self.check_pat_struct(pat, qpath, fields, etc, expected, def_bm, discrim_span)
@@ -1054,13 +1056,14 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
10541056
fn check_pat_path(
10551057
&self,
10561058
pat: &hir::Pat,
1059+
path_resolution: (Res, Option<Ty<'tcx>>, &'b [hir::PathSegment]),
10571060
qpath: &hir::QPath,
10581061
expected: Ty<'tcx>,
10591062
) -> Ty<'tcx> {
10601063
let tcx = self.tcx;
10611064

1062-
// Resolve the path and check the definition for errors.
1063-
let (res, opt_ty, segments) = self.resolve_ty_and_res_ufcs(qpath, pat.hir_id, pat.span);
1065+
// We have already resolved the path.
1066+
let (res, opt_ty, segments) = path_resolution;
10641067
match res {
10651068
Res::Err => {
10661069
self.set_tainted_by_errors();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// In this regression test we check that a path pattern referring to a unit variant
2+
// through a type alias is successful in inferring the generic argument.
3+
4+
// compile-pass
5+
6+
#![feature(type_alias_enum_variants)]
7+
8+
enum Opt<T> {
9+
N,
10+
S(T),
11+
}
12+
13+
type OptAlias<T> = Opt<T>;
14+
15+
fn f1(x: OptAlias<u8>) {
16+
match x {
17+
OptAlias::N // We previously failed to infer `T` to `u8`.
18+
=> (),
19+
_ => (),
20+
}
21+
22+
match x {
23+
<
24+
OptAlias<_> // And we failed to infer this type also.
25+
>::N => (),
26+
_ => (),
27+
}
28+
}
29+
30+
fn main() {}

0 commit comments

Comments
 (0)