From d3024138f8e49e815a205dcb80e2e21decc57aa6 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Fri, 14 Jun 2019 09:00:21 +0200 Subject: [PATCH 1/2] PatKind::Path: avoid calling resolve_ty_and_res_ufcs twice. --- src/librustc_typeck/check/_match.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs index b882b696938ac..d27c62037631a 100644 --- a/src/librustc_typeck/check/_match.rs +++ b/src/librustc_typeck/check/_match.rs @@ -50,6 +50,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { debug!("check_pat_walk(pat={:?},expected={:?},def_bm={:?})", pat, expected, def_bm); + let mut path_resolution = None; let is_non_ref_pat = match pat.node { PatKind::Struct(..) | PatKind::TupleStruct(..) | @@ -65,8 +66,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } } PatKind::Path(ref qpath) => { - let (def, _, _) = self.resolve_ty_and_res_ufcs(qpath, pat.hir_id, pat.span); - match def { + let resolution = self.resolve_ty_and_res_ufcs(qpath, pat.hir_id, pat.span); + path_resolution = Some(resolution); + match resolution.0 { Res::Def(DefKind::Const, _) | Res::Def(DefKind::AssocConst, _) => false, _ => true, } @@ -294,7 +296,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { ) } PatKind::Path(ref qpath) => { - self.check_pat_path(pat, qpath, expected) + self.check_pat_path(pat, path_resolution.unwrap(), qpath, expected) } PatKind::Struct(ref qpath, ref fields, etc) => { self.check_pat_struct(pat, qpath, fields, etc, expected, def_bm, discrim_span) @@ -1055,13 +1057,14 @@ https://doc.rust-lang.org/reference/types.html#trait-objects"); fn check_pat_path( &self, pat: &hir::Pat, + path_resolution: (Res, Option>, &'b [hir::PathSegment]), qpath: &hir::QPath, expected: Ty<'tcx>, ) -> Ty<'tcx> { let tcx = self.tcx; - // Resolve the path and check the definition for errors. - let (res, opt_ty, segments) = self.resolve_ty_and_res_ufcs(qpath, pat.hir_id, pat.span); + // We have already resolved the path. + let (res, opt_ty, segments) = path_resolution; match res { Res::Err => { self.set_tainted_by_errors(); From 065151f8b2c31d9e4ddd34aaf8d3263a997f5cfe Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Fri, 14 Jun 2019 09:16:39 +0200 Subject: [PATCH 2/2] type_alias_enum_variants: add regression test for #61801. --- .../issue-61801-path-pattern-can-infer.rs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/test/ui/type-alias-enum-variants/issue-61801-path-pattern-can-infer.rs diff --git a/src/test/ui/type-alias-enum-variants/issue-61801-path-pattern-can-infer.rs b/src/test/ui/type-alias-enum-variants/issue-61801-path-pattern-can-infer.rs new file mode 100644 index 0000000000000..21be61acb0c61 --- /dev/null +++ b/src/test/ui/type-alias-enum-variants/issue-61801-path-pattern-can-infer.rs @@ -0,0 +1,30 @@ +// In this regression test we check that a path pattern referring to a unit variant +// through a type alias is successful in inferring the generic argument. + +// compile-pass + +#![feature(type_alias_enum_variants)] + +enum Opt { + N, + S(T), +} + +type OptAlias = Opt; + +fn f1(x: OptAlias) { + match x { + OptAlias::N // We previously failed to infer `T` to `u8`. + => (), + _ => (), + } + + match x { + < + OptAlias<_> // And we failed to infer this type also. + >::N => (), + _ => (), + } +} + +fn main() {}