Skip to content

Commit

Permalink
Start handling pattern types at the HIR -> Ty conversion boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jan 22, 2024
1 parent 31dbd68 commit fcd68c3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,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
Expand Down
22 changes: 20 additions & 2 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::astconv::errors::prohibit_assoc_ty_binding;
use crate::astconv::generics::{check_generic_arg_count, create_args_for_parent_generic_args};
use crate::bounds::Bounds;
use crate::collect::HirPlaceholderCollector;
use crate::errors::{AmbiguousLifetimeBound, TypeofReservedKeywordUsed};
use crate::errors::{AmbiguousLifetimeBound, TypeofReservedKeywordUsed, WildPatTy};
use crate::middle::resolve_bound_vars as rbv;
use crate::require_c_abi_if_c_variadic;
use rustc_ast::TraitObjectSyntax;
Expand Down Expand Up @@ -2559,7 +2559,25 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
// handled specially and will not descend into this routine.
self.ty_infer(None, ast_ty.span)
}
hir::TyKind::Pat(..) => span_bug!(ast_ty.span, "{ast_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::Binding(_, _, _, _) => todo!(),
hir::PatKind::Struct(_, _, _) => todo!(),
hir::PatKind::TupleStruct(_, _, _) => todo!(),
hir::PatKind::Or(_) => todo!(),
hir::PatKind::Path(_) => todo!(),
hir::PatKind::Tuple(_, _) => todo!(),
hir::PatKind::Box(_) => todo!(),
hir::PatKind::Ref(_, _) => todo!(),
hir::PatKind::Lit(_) => todo!(),
hir::PatKind::Range(_, _, _) => Ty::new_misc_error(tcx),
hir::PatKind::Slice(_, _, _) => todo!(),
hir::PatKind::Never => todo!(),
hir::PatKind::Err(e) => Ty::new_error(tcx, e),
},
hir::TyKind::Err(guar) => Ty::new_error(tcx, *guar),
};

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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)]
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_hir_analysis/src/errors/pattern_types.rs
Original file line number Diff line number Diff line change
@@ -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,
}
6 changes: 4 additions & 2 deletions tests/ui/type/pattern_types/bad_pat.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// compile-flags: -Zno-analysis

#![feature(pattern_types)]
#![feature(core_pattern_types)]
#![feature(core_pattern_type)]
Expand All @@ -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() {}
12 changes: 9 additions & 3 deletions tests/ui/type/pattern_types/bad_pat.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
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
|
= 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`.

0 comments on commit fcd68c3

Please sign in to comment.