Skip to content

Commit bd41537

Browse files
committed
Actually create ranged int types in the type system.
1 parent ff28254 commit bd41537

File tree

56 files changed

+451
-43
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+451
-43
lines changed

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ fn push_debuginfo_type_name<'tcx>(
194194
}
195195
}
196196
}
197+
ty::Pat(inner_type, pat) => {
198+
if cpp_like_debuginfo {
199+
output.push_str("pat$<");
200+
push_debuginfo_type_name(tcx, inner_type, true, output, visited);
201+
write!(output, ",{:?}>", pat).unwrap();
202+
} else {
203+
write!(output, "{:?}", t).unwrap();
204+
}
205+
}
197206
ty::Slice(inner_type) => {
198207
if cpp_like_debuginfo {
199208
output.push_str("slice2$<");

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ pub(crate) fn const_to_valtree_inner<'tcx>(
9696
Ok(ty::ValTree::Leaf(val.assert_int()))
9797
}
9898

99+
ty::Pat(..) => const_to_valtree_inner(ecx, &ecx.mplace_field(&place, 0).unwrap(), num_nodes),
100+
99101
// Raw pointers are not allowed in type level constants, as we cannot properly test them for
100102
// equality at compile-time (see `ptr_guaranteed_cmp`).
101103
// Technically we could allow function pointers (represented as `ty::Instance`), but this is not guaranteed to
@@ -265,7 +267,7 @@ pub fn valtree_to_const_value<'tcx>(
265267
let (param_env, ty) = param_env_ty.into_parts();
266268
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
267269

268-
match ty.kind() {
270+
match *ty.kind() {
269271
ty::FnDef(..) => {
270272
assert!(valtree.unwrap_branch().is_empty());
271273
ConstValue::ZeroSized
@@ -276,6 +278,7 @@ pub fn valtree_to_const_value<'tcx>(
276278
"ValTrees for Bool, Int, Uint, Float or Char should have the form ValTree::Leaf"
277279
),
278280
},
281+
ty::Pat(ty, _) => valtree_to_const_value(tcx, param_env.and(ty), valtree),
279282
ty::Ref(_, _, _) | ty::Tuple(_) | ty::Array(_, _) | ty::Adt(..) => {
280283
let mut place = match ty.kind() {
281284
ty::Ref(_, inner_ty, _) => {

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
8383
ty::Alias(..) | ty::Param(_) | ty::Placeholder(_) | ty::Infer(_) => {
8484
throw_inval!(TooGeneric)
8585
}
86+
ty::Pat(..) => {
87+
unimplemented!("pattern types need to calculate pattern from their pattern")
88+
}
8689
ty::Bound(_, _) => bug!("bound ty during ctfe"),
8790
ty::Bool
8891
| ty::Char

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
584584
// Nothing to check.
585585
Ok(true)
586586
}
587+
ty::Pat(..) => unimplemented!(),
587588
// The above should be all the primitive types. The rest is compound, we
588589
// check them by visiting their fields/variants.
589590
ty::Adt(..)

compiler/rustc_const_eval/src/util/type_name.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
4040
| ty::Uint(_)
4141
| ty::Float(_)
4242
| ty::Str
43+
| ty::Pat(_, _)
4344
| ty::Array(_, _)
4445
| ty::Slice(_)
4546
| ty::RawPtr(_)

compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ hir_analysis_auto_deref_reached_recursion_limit = reached the recursion limit wh
127127
128128
hir_analysis_pattern_type_wild_pat = "wildcard patterns are not permitted for pattern types"
129129
.label = "this type is the same as the inner type without a pattern"
130+
131+
hir_analysis_pattern_type_non_const_range = "range patterns must have constant range start and end"

compiler/rustc_error_messages/locales/en-US/lint.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ lint_improper_ctypes_128bit = 128-bit integers don't currently have a known stab
239239
lint_improper_ctypes_char_reason = the `char` type has no C equivalent
240240
lint_improper_ctypes_char_help = consider using `u32` or `libc::wchar_t` instead
241241
242+
lint_improper_ctypes_pat_reason = pattern types have no C equivalent
243+
lint_improper_ctypes_pat_help = consider using the patterned type instead
244+
242245
lint_improper_ctypes_non_exhaustive = this enum is non-exhaustive
243246
lint_improper_ctypes_non_exhaustive_variant = this enum has non-exhaustive variants
244247

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use crate::astconv::generics::{
1111
use crate::bounds::Bounds;
1212
use crate::collect::HirPlaceholderCollector;
1313
use crate::errors::{
14-
AmbiguousLifetimeBound, MultipleRelaxedDefaultBounds, TraitObjectDeclaredWithNoTraits,
15-
TypeofReservedKeywordUsed, ValueOfAssociatedStructAlreadySpecified, WildPatTy,
14+
AmbiguousLifetimeBound, MultipleRelaxedDefaultBounds, NonConstRange,
15+
TraitObjectDeclaredWithNoTraits, TypeofReservedKeywordUsed,
16+
ValueOfAssociatedStructAlreadySpecified, WildPatTy,
1617
};
1718
use crate::middle::resolve_lifetime as rl;
1819
use crate::require_c_abi_if_c_variadic;
@@ -29,6 +30,7 @@ use rustc_hir::intravisit::{walk_generics, Visitor as _};
2930
use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin};
3031
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
3132
use rustc_middle::middle::stability::AllowUnstable;
33+
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
3234
use rustc_middle::ty::subst::{self, GenericArgKind, InternalSubsts, SubstsRef};
3335
use rustc_middle::ty::GenericParamDefKind;
3436
use rustc_middle::ty::{self, Const, DefIdTree, IsSuggestable, Ty, TyCtxt, TypeVisitable};
@@ -2968,23 +2970,72 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
29682970
// handled specially and will not descend into this routine.
29692971
self.ty_infer(None, ast_ty.span)
29702972
}
2971-
hir::TyKind::Pat(_ty, pat) => match pat.kind {
2972-
hir::PatKind::Wild => {
2973-
let err = tcx.sess.emit_err(WildPatTy { span: pat.span });
2974-
tcx.ty_error_with_guaranteed(err)
2975-
}
2976-
hir::PatKind::Binding(_, _, _, _) => todo!(),
2977-
hir::PatKind::Struct(_, _, _) => todo!(),
2978-
hir::PatKind::TupleStruct(_, _, _) => todo!(),
2979-
hir::PatKind::Or(_) => todo!(),
2980-
hir::PatKind::Path(_) => todo!(),
2981-
hir::PatKind::Tuple(_, _) => todo!(),
2982-
hir::PatKind::Box(_) => todo!(),
2983-
hir::PatKind::Ref(_, _) => todo!(),
2984-
hir::PatKind::Lit(_) => todo!(),
2985-
hir::PatKind::Range(_, _, _) => tcx.ty_error(),
2986-
hir::PatKind::Slice(_, _, _) => todo!(),
2987-
},
2973+
hir::TyKind::Pat(ty, pat) => {
2974+
let ty = self.ast_ty_to_ty(ty);
2975+
let pat_ty = match pat.kind {
2976+
hir::PatKind::Wild => {
2977+
let err = tcx.sess.emit_err(WildPatTy { span: pat.span });
2978+
tcx.ty_error_with_guaranteed(err)
2979+
}
2980+
hir::PatKind::Binding(_, _, _, _) => todo!(),
2981+
hir::PatKind::Struct(_, _, _) => todo!(),
2982+
hir::PatKind::TupleStruct(_, _, _) => todo!(),
2983+
hir::PatKind::Or(_) => todo!(),
2984+
hir::PatKind::Path(_) => todo!(),
2985+
hir::PatKind::Tuple(_, _) => todo!(),
2986+
hir::PatKind::Box(_) => todo!(),
2987+
hir::PatKind::Ref(_, _) => todo!(),
2988+
hir::PatKind::Lit(_) => todo!(),
2989+
hir::PatKind::Range(start, end, include_end) => {
2990+
let expr_to_const = |expr: &'tcx hir::Expr<'tcx>, neg| -> ty::Const<'tcx> {
2991+
match &expr.kind {
2992+
hir::ExprKind::Lit(lit) => {
2993+
let lit_input = LitToConstInput { lit: &lit.node, ty, neg };
2994+
match tcx.lit_to_const(lit_input) {
2995+
Ok(c) => c,
2996+
Err(LitToConstError::Reported(err)) => {
2997+
tcx.const_error_with_guaranteed(ty, err)
2998+
}
2999+
Err(LitToConstError::TypeError) => todo!(),
3000+
}
3001+
}
3002+
_ => {
3003+
let err = tcx.sess.emit_err(NonConstRange { span: expr.span });
3004+
tcx.const_error_with_guaranteed(ty, err)
3005+
}
3006+
}
3007+
};
3008+
let expr_to_const = |expr, neg| {
3009+
let c = expr_to_const(expr, neg);
3010+
self.record_ty(expr.hir_id, c.ty(), expr.span);
3011+
c
3012+
};
3013+
let expr_to_const = |expr: &'tcx hir::Expr<'tcx>| match &expr.kind {
3014+
hir::ExprKind::Unary(hir::UnOp::Neg, expr) => expr_to_const(expr, true),
3015+
_ => expr_to_const(expr, false),
3016+
};
3017+
let expr_to_const = |expr| {
3018+
let c = expr_to_const(expr);
3019+
self.record_ty(expr.hir_id, c.ty(), expr.span);
3020+
c
3021+
};
3022+
3023+
let start = start.map(expr_to_const);
3024+
let end = end.map(expr_to_const);
3025+
3026+
let include_end = match include_end {
3027+
hir::RangeEnd::Included => true,
3028+
hir::RangeEnd::Excluded => false,
3029+
};
3030+
3031+
let pat = tcx.mk_pat(ty::PatternKind::Range { start, end, include_end });
3032+
tcx.mk_ty(ty::Pat(ty, pat))
3033+
}
3034+
hir::PatKind::Slice(_, _, _) => todo!(),
3035+
};
3036+
self.record_ty(pat.hir_id, ty, pat.span);
3037+
pat_ty
3038+
}
29883039
hir::TyKind::Err => tcx.ty_error(),
29893040
};
29903041

compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ impl<'tcx> InherentCollect<'tcx> {
208208
.note("define and implement a new trait or type instead")
209209
.emit();
210210
}
211+
ty::Pat(..) => {
212+
self.tcx.sess.span_err(ty.span, "cannot define inherent `impl` for pattern types");
213+
}
211214
ty::Bool
212215
| ty::Char
213216
| ty::Int(_)

compiler/rustc_hir_analysis/src/errors/pattern_types.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ pub struct WildPatTy {
77
#[primary_span]
88
pub span: Span,
99
}
10+
11+
#[derive(Diagnostic)]
12+
#[diag(hir_analysis_pattern_type_non_const_range)]
13+
pub struct NonConstRange {
14+
#[primary_span]
15+
pub span: Span,
16+
}

0 commit comments

Comments
 (0)