1
Fork 0

Rollup merge of #137715 - oli-obk:pattern-type-literals, r=BoxyUwU

Allow int literals for pattern types with int base types

r? ``@BoxyUwU``

I also added an error at layout computation time for layouts that contain wrapping ranges (happens at monomorphization time). This is obviously hacky, but at least prevents such types from making it to codegen for now. It made writing the tests for int literals easier as I didn't have to think about that edge case

Basically this PR allows you to stop using transmutes for creating pattern types and instead just use literals:

```rust
let x: pattern_type!(u32 is 5..10) = 7;
```

works, and if the literal is out of range you get a type mismatch because it just stays at the base type and the base type can't be coerced to the pattern type.

cc ``@joshtriplett`` ``@scottmcm``
This commit is contained in:
Matthias Krüger 2025-03-11 19:35:27 +01:00 committed by GitHub
commit 8a2e3acb45
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 518 additions and 12 deletions

View file

@ -117,7 +117,12 @@ fn lit_to_mir_constant<'tcx>(tcx: TyCtxt<'tcx>, lit_input: LitToConstInput<'tcx>
ConstValue::Scalar(Scalar::from_uint(result, width))
};
let value = match (lit, ty.kind()) {
let lit_ty = match *ty.kind() {
ty::Pat(base, _) => base,
_ => ty,
};
let value = match (lit, lit_ty.kind()) {
(ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => {
let s = s.as_str();
let allocation = Allocation::from_bytes_byte_aligned_immutable(s.as_bytes());