diff --git a/compiler/rustc_hir_analysis/messages.ftl b/compiler/rustc_hir_analysis/messages.ftl index d8a90d62dac..22dc878d3d8 100644 --- a/compiler/rustc_hir_analysis/messages.ftl +++ b/compiler/rustc_hir_analysis/messages.ftl @@ -349,6 +349,8 @@ hir_analysis_pass_to_variadic_function = can't pass `{$ty}` to variadic function .suggestion = cast the value to `{$cast_ty}` .help = cast the value to `{$cast_ty}` +hir_analysis_pattern_type_wild_pat = "wildcard patterns are not permitted for pattern types" + .label = "this type is the same as the inner type without a pattern" hir_analysis_placeholder_not_allowed_item_signatures = the placeholder `_` is not allowed within types on item signatures for {$kind} .label = not allowed in type signatures diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs index 2d4742fa1dc..adb5cd24929 100644 --- a/compiler/rustc_hir_analysis/src/errors.rs +++ b/compiler/rustc_hir_analysis/src/errors.rs @@ -7,6 +7,8 @@ use rustc_errors::{ use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_middle::ty::Ty; use rustc_span::{symbol::Ident, Span, Symbol}; +mod pattern_types; +pub use pattern_types::*; #[derive(Diagnostic)] #[diag(hir_analysis_ambiguous_assoc_item)] diff --git a/compiler/rustc_hir_analysis/src/errors/pattern_types.rs b/compiler/rustc_hir_analysis/src/errors/pattern_types.rs new file mode 100644 index 00000000000..008d2698989 --- /dev/null +++ b/compiler/rustc_hir_analysis/src/errors/pattern_types.rs @@ -0,0 +1,9 @@ +use rustc_macros::Diagnostic; +use rustc_span::Span; + +#[derive(Diagnostic)] +#[diag(hir_analysis_pattern_type_wild_pat)] +pub struct WildPatTy { + #[primary_span] + pub span: Span, +} diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 528af8c1ce8..8ff5a22c484 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -21,7 +21,7 @@ mod object_safety; use crate::bounds::Bounds; use crate::collect::HirPlaceholderCollector; -use crate::errors::AmbiguousLifetimeBound; +use crate::errors::{AmbiguousLifetimeBound, WildPatTy}; use crate::hir_ty_lowering::errors::{prohibit_assoc_item_binding, GenericsArgsErrExtend}; use crate::hir_ty_lowering::generics::{check_generic_arg_count, lower_generic_args}; use crate::middle::resolve_bound_vars as rbv; @@ -2195,7 +2195,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { // handled specially and will not descend into this routine. self.ty_infer(None, hir_ty.span) } - hir::TyKind::Pat(..) => span_bug!(hir_ty.span, "{hir_ty:#?}"), + hir::TyKind::Pat(_ty, pat) => match pat.kind { + hir::PatKind::Wild => { + let err = tcx.dcx().emit_err(WildPatTy { span: pat.span }); + Ty::new_error(tcx, err) + } + hir::PatKind::Range(_, _, _) => Ty::new_misc_error(tcx), + hir::PatKind::Err(e) => Ty::new_error(tcx, e), + _ => span_bug!(pat.span, "unsupported pattern for pattern type: {pat:#?}"), + }, hir::TyKind::Err(guar) => Ty::new_error(tcx, *guar), }; diff --git a/tests/ui/type/pattern_types/bad_pat.rs b/tests/ui/type/pattern_types/bad_pat.rs index 4b19f610118..8ad042eeba6 100644 --- a/tests/ui/type/pattern_types/bad_pat.rs +++ b/tests/ui/type/pattern_types/bad_pat.rs @@ -1,5 +1,3 @@ -//@ compile-flags: -Zno-analysis - #![feature(pattern_types)] #![feature(core_pattern_types)] #![feature(core_pattern_type)] @@ -10,3 +8,7 @@ type NonNullU32_2 = pattern_type!(u32 is 1..=); //~^ ERROR: inclusive range with no end type Positive2 = pattern_type!(i32 is 0..=); //~^ ERROR: inclusive range with no end +type Wild = pattern_type!(() is _); +//~^ ERROR: wildcard patterns are not permitted for pattern types + +fn main() {} diff --git a/tests/ui/type/pattern_types/bad_pat.stderr b/tests/ui/type/pattern_types/bad_pat.stderr index 12fcaf42b8e..4f0f0bc9742 100644 --- a/tests/ui/type/pattern_types/bad_pat.stderr +++ b/tests/ui/type/pattern_types/bad_pat.stderr @@ -1,5 +1,5 @@ error[E0586]: inclusive range with no end - --> $DIR/bad_pat.rs:9:43 + --> $DIR/bad_pat.rs:7:43 | LL | type NonNullU32_2 = pattern_type!(u32 is 1..=); | ^^^ help: use `..` instead @@ -7,13 +7,19 @@ LL | type NonNullU32_2 = pattern_type!(u32 is 1..=); = note: inclusive ranges must be bounded at the end (`..=b` or `a..=b`) error[E0586]: inclusive range with no end - --> $DIR/bad_pat.rs:11:40 + --> $DIR/bad_pat.rs:9:40 | LL | type Positive2 = pattern_type!(i32 is 0..=); | ^^^ help: use `..` instead | = note: inclusive ranges must be bounded at the end (`..=b` or `a..=b`) -error: aborting due to 2 previous errors +error: "wildcard patterns are not permitted for pattern types" + --> $DIR/bad_pat.rs:11:33 + | +LL | type Wild = pattern_type!(() is _); + | ^ + +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0586`.