Skip to content

Commit 1d6cd8d

Browse files
committed
Start handling pattern types at the HIR -> Ty conversion boundary
1 parent c4efc25 commit 1d6cd8d

File tree

6 files changed

+36
-7
lines changed

6 files changed

+36
-7
lines changed

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ hir_analysis_pass_to_variadic_function = can't pass `{$ty}` to variadic function
349349
.suggestion = cast the value to `{$cast_ty}`
350350
.help = cast the value to `{$cast_ty}`
351351
352+
hir_analysis_pattern_type_wild_pat = "wildcard patterns are not permitted for pattern types"
353+
.label = "this type is the same as the inner type without a pattern"
352354
hir_analysis_placeholder_not_allowed_item_signatures = the placeholder `_` is not allowed within types on item signatures for {$kind}
353355
.label = not allowed in type signatures
354356

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use rustc_errors::{
77
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
88
use rustc_middle::ty::Ty;
99
use rustc_span::{symbol::Ident, Span, Symbol};
10+
mod pattern_types;
11+
pub use pattern_types::*;
1012

1113
#[derive(Diagnostic)]
1214
#[diag(hir_analysis_ambiguous_assoc_item)]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use rustc_macros::Diagnostic;
2+
use rustc_span::Span;
3+
4+
#[derive(Diagnostic)]
5+
#[diag(hir_analysis_pattern_type_wild_pat)]
6+
pub struct WildPatTy {
7+
#[primary_span]
8+
pub span: Span,
9+
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod object_safety;
2121

2222
use crate::bounds::Bounds;
2323
use crate::collect::HirPlaceholderCollector;
24-
use crate::errors::AmbiguousLifetimeBound;
24+
use crate::errors::{AmbiguousLifetimeBound, WildPatTy};
2525
use crate::hir_ty_lowering::errors::{prohibit_assoc_item_binding, GenericsArgsErrExtend};
2626
use crate::hir_ty_lowering::generics::{check_generic_arg_count, lower_generic_args};
2727
use crate::middle::resolve_bound_vars as rbv;
@@ -2195,7 +2195,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21952195
// handled specially and will not descend into this routine.
21962196
self.ty_infer(None, hir_ty.span)
21972197
}
2198-
hir::TyKind::Pat(..) => span_bug!(hir_ty.span, "{hir_ty:#?}"),
2198+
hir::TyKind::Pat(_ty, pat) => match pat.kind {
2199+
hir::PatKind::Wild => {
2200+
let err = tcx.dcx().emit_err(WildPatTy { span: pat.span });
2201+
Ty::new_error(tcx, err)
2202+
}
2203+
hir::PatKind::Range(_, _, _) => Ty::new_misc_error(tcx),
2204+
hir::PatKind::Err(e) => Ty::new_error(tcx, e),
2205+
_ => span_bug!(pat.span, "unsupported pattern for pattern type: {pat:#?}"),
2206+
},
21992207
hir::TyKind::Err(guar) => Ty::new_error(tcx, *guar),
22002208
};
22012209

tests/ui/type/pattern_types/bad_pat.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@ compile-flags: -Zno-analysis
2-
31
#![feature(pattern_types)]
42
#![feature(core_pattern_types)]
53
#![feature(core_pattern_type)]
@@ -10,3 +8,7 @@ type NonNullU32_2 = pattern_type!(u32 is 1..=);
108
//~^ ERROR: inclusive range with no end
119
type Positive2 = pattern_type!(i32 is 0..=);
1210
//~^ ERROR: inclusive range with no end
11+
type Wild = pattern_type!(() is _);
12+
//~^ ERROR: wildcard patterns are not permitted for pattern types
13+
14+
fn main() {}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
error[E0586]: inclusive range with no end
2-
--> $DIR/bad_pat.rs:9:43
2+
--> $DIR/bad_pat.rs:7:43
33
|
44
LL | type NonNullU32_2 = pattern_type!(u32 is 1..=);
55
| ^^^ help: use `..` instead
66
|
77
= note: inclusive ranges must be bounded at the end (`..=b` or `a..=b`)
88

99
error[E0586]: inclusive range with no end
10-
--> $DIR/bad_pat.rs:11:40
10+
--> $DIR/bad_pat.rs:9:40
1111
|
1212
LL | type Positive2 = pattern_type!(i32 is 0..=);
1313
| ^^^ help: use `..` instead
1414
|
1515
= note: inclusive ranges must be bounded at the end (`..=b` or `a..=b`)
1616

17-
error: aborting due to 2 previous errors
17+
error: "wildcard patterns are not permitted for pattern types"
18+
--> $DIR/bad_pat.rs:11:33
19+
|
20+
LL | type Wild = pattern_type!(() is _);
21+
| ^
22+
23+
error: aborting due to 3 previous errors
1824

1925
For more information about this error, try `rustc --explain E0586`.

0 commit comments

Comments
 (0)