Skip to content

Commit c476958

Browse files
committed
Start handling pattern types at the HIR -> Ty conversion boundary
1 parent 27e4e32 commit c476958

File tree

6 files changed

+46
-7
lines changed

6 files changed

+46
-7
lines changed

compiler/rustc_hir_analysis/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ hir_analysis_pass_to_variadic_function = can't pass `{$ty}` to variadic function
299299
.suggestion = cast the value to `{$cast_ty}`
300300
.help = cast the value to `{$cast_ty}`
301301
302+
hir_analysis_pattern_type_wild_pat = "wildcard patterns are not permitted for pattern types"
303+
.label = "this type is the same as the inner type without a pattern"
302304
hir_analysis_placeholder_not_allowed_item_signatures = the placeholder `_` is not allowed within types on item signatures for {$kind}
303305
.label = not allowed in type signatures
304306

compiler/rustc_hir_analysis/src/astconv/mod.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::astconv::errors::prohibit_assoc_ty_binding;
1212
use crate::astconv::generics::{check_generic_arg_count, create_args_for_parent_generic_args};
1313
use crate::bounds::Bounds;
1414
use crate::collect::HirPlaceholderCollector;
15-
use crate::errors::{AmbiguousLifetimeBound, TypeofReservedKeywordUsed};
15+
use crate::errors::{AmbiguousLifetimeBound, TypeofReservedKeywordUsed, WildPatTy};
1616
use crate::middle::resolve_bound_vars as rbv;
1717
use crate::require_c_abi_if_c_variadic;
1818
use rustc_ast::TraitObjectSyntax;
@@ -2559,7 +2559,25 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
25592559
// handled specially and will not descend into this routine.
25602560
self.ty_infer(None, ast_ty.span)
25612561
}
2562-
hir::TyKind::Pat(..) => span_bug!(ast_ty.span, "{ast_ty:#?}"),
2562+
hir::TyKind::Pat(_ty, pat) => match pat.kind {
2563+
hir::PatKind::Wild => {
2564+
let err = tcx.dcx().emit_err(WildPatTy { span: pat.span });
2565+
Ty::new_error(tcx, err)
2566+
}
2567+
hir::PatKind::Binding(_, _, _, _) => todo!(),
2568+
hir::PatKind::Struct(_, _, _) => todo!(),
2569+
hir::PatKind::TupleStruct(_, _, _) => todo!(),
2570+
hir::PatKind::Or(_) => todo!(),
2571+
hir::PatKind::Path(_) => todo!(),
2572+
hir::PatKind::Tuple(_, _) => todo!(),
2573+
hir::PatKind::Box(_) => todo!(),
2574+
hir::PatKind::Ref(_, _) => todo!(),
2575+
hir::PatKind::Lit(_) => todo!(),
2576+
hir::PatKind::Range(_, _, _) => Ty::new_misc_error(tcx),
2577+
hir::PatKind::Slice(_, _, _) => todo!(),
2578+
hir::PatKind::Never => todo!(),
2579+
hir::PatKind::Err(e) => Ty::new_error(tcx, e),
2580+
},
25632581
hir::TyKind::Err(guar) => Ty::new_error(tcx, *guar),
25642582
};
25652583

compiler/rustc_hir_analysis/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use rustc_errors::{
88
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
99
use rustc_middle::ty::Ty;
1010
use rustc_span::{symbol::Ident, Span, Symbol};
11+
mod pattern_types;
12+
pub use pattern_types::*;
1113

1214
#[derive(Diagnostic)]
1315
#[diag(hir_analysis_ambiguous_assoc_item)]
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+
}

tests/ui/type/pattern_types/bad_pat.rs

+4-2
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() {}
+9-3
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)