Skip to content

Commit 220953b

Browse files
authored
Merge pull request #442 from nathanwhit/typename-raw-pointer
Add raw pointers to `TypeName`
2 parents ff137d4 + dc3a03b commit 220953b

File tree

12 files changed

+88
-4
lines changed

12 files changed

+88
-4
lines changed

book/src/clauses/well_known_traits.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Some common examples of auto traits are `Send` and `Sync`.
3535
| scalar types | 📚 | 📚 ||||||||
3636
| trait objects ||||||||||
3737
| functions ptrs ||||||||||
38+
| raw ptrs ||||||||||
3839
| arrays❌ ||||||||||
3940
| slices❌ ||||||||||
4041
| closures❌ ||||||||||

chalk-integration/src/lowering.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,17 @@ impl LowerTy for Ty {
12101210
substitution: chalk_ir::Substitution::empty(interner),
12111211
})
12121212
.intern(interner)),
1213+
1214+
Ty::Raw { mutability, ty } => Ok(chalk_ir::TyData::Apply(chalk_ir::ApplicationTy {
1215+
name: chalk_ir::TypeName::Raw(ast_mutability_to_chalk_mutability(
1216+
mutability.clone(),
1217+
)),
1218+
substitution: chalk_ir::Substitution::from_fallible(
1219+
interner,
1220+
std::iter::once(Ok(ty.lower(env)?)),
1221+
)?,
1222+
})
1223+
.intern(interner)),
12131224
}
12141225
}
12151226
}
@@ -1591,3 +1602,10 @@ fn ast_scalar_to_chalk_scalar(scalar: ScalarType) -> chalk_ir::Scalar {
15911602
ScalarType::Char => chalk_ir::Scalar::Char,
15921603
}
15931604
}
1605+
1606+
fn ast_mutability_to_chalk_mutability(mutability: Mutability) -> chalk_ir::Mutability {
1607+
match mutability {
1608+
Mutability::Mut => chalk_ir::Mutability::Mut,
1609+
Mutability::Not => chalk_ir::Mutability::Not,
1610+
}
1611+
}

chalk-ir/src/debug.rs

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl<I: Interner> Debug for TypeName<I> {
144144
TypeName::Scalar(scalar) => write!(fmt, "{:?}", scalar),
145145
TypeName::Tuple(arity) => write!(fmt, "{:?}", arity),
146146
TypeName::OpaqueType(opaque_ty) => write!(fmt, "!{:?}", opaque_ty),
147+
TypeName::Raw(mutability) => write!(fmt, "{:?}", mutability),
147148
TypeName::Error => write!(fmt, "{{error}}"),
148149
}
149150
}

chalk-ir/src/fold/boring_impls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ copy_fold!(IntTy);
248248
copy_fold!(FloatTy);
249249
copy_fold!(Scalar);
250250
copy_fold!(ClausePriority);
251+
copy_fold!(Mutability);
251252

252253
#[macro_export]
253254
macro_rules! id_fold {

chalk-ir/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ pub enum Scalar {
136136
Float(FloatTy),
137137
}
138138

139+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
140+
pub enum Mutability {
141+
Mut,
142+
Not,
143+
}
144+
139145
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Fold, Visit)]
140146
pub enum TypeName<I: Interner> {
141147
/// a type like `Vec<T>`
@@ -150,6 +156,9 @@ pub enum TypeName<I: Interner> {
150156
/// a tuple of the given arity
151157
Tuple(usize),
152158

159+
/// a raw pointer type like `*const T` or `*mut T`
160+
Raw(Mutability),
161+
153162
/// a placeholder for opaque types like `impl Trait`
154163
OpaqueType(OpaqueTyId<I>),
155164

chalk-ir/src/visit/boring_impls.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
77
use crate::{
88
AssocTypeId, ClausePriority, DebruijnIndex, FloatTy, Goals, ImplId, IntTy, Interner,
9-
OpaqueTyId, Parameter, ParameterKind, PlaceholderIndex, ProgramClause, ProgramClauseData,
10-
ProgramClauses, QuantifiedWhereClauses, QuantifierKind, Scalar, StructId, Substitution,
11-
SuperVisit, TraitId, UintTy, UniverseIndex, Visit, VisitResult, Visitor,
9+
Mutability, OpaqueTyId, Parameter, ParameterKind, PlaceholderIndex, ProgramClause,
10+
ProgramClauseData, ProgramClauses, QuantifiedWhereClauses, QuantifierKind, Scalar, StructId,
11+
Substitution, SuperVisit, TraitId, UintTy, UniverseIndex, Visit, VisitResult, Visitor,
1212
};
1313
use chalk_engine::{context::Context, ExClause, FlounderedSubgoal, Literal};
1414
use std::{marker::PhantomData, sync::Arc};
@@ -211,6 +211,7 @@ const_visit!(Scalar);
211211
const_visit!(UintTy);
212212
const_visit!(IntTy);
213213
const_visit!(FloatTy);
214+
const_visit!(Mutability);
214215

215216
#[macro_export]
216217
macro_rules! id_visit {

chalk-parse/src/ast.rs

+10
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ pub enum Ty {
193193
Scalar {
194194
ty: ScalarType,
195195
},
196+
Raw {
197+
mutability: Mutability,
198+
ty: Box<Ty>,
199+
},
196200
}
197201

198202
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
@@ -230,6 +234,12 @@ pub enum ScalarType {
230234
Float(FloatTy),
231235
}
232236

237+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
238+
pub enum Mutability {
239+
Mut,
240+
Not,
241+
}
242+
233243
#[derive(Clone, PartialEq, Eq, Debug)]
234244
pub enum Lifetime {
235245
Id { name: Identifier },

chalk-parse/src/parser.lalrpop

+6
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ TyWithoutFor: Ty = {
205205
<n:Id> "<" <a:Comma<Parameter>> ">" => Ty::Apply { name: n, args: a },
206206
<p:ProjectionTy> => Ty::Projection { proj: p },
207207
"(" <t:TupleOrParensInner> ")" => t,
208+
"*" <m: Mutability> <t:Ty> => Ty::Raw{ mutability: m, ty: Box::new(t) },
208209
};
209210

210211
ScalarType: ScalarType = {
@@ -237,6 +238,11 @@ TupleOrParensInner: Ty = {
237238
() => Ty::Tuple { types: vec![] },
238239
};
239240

241+
Mutability: Mutability = {
242+
"mut" => Mutability::Mut,
243+
"const" => Mutability::Not,
244+
};
245+
240246
Lifetime: Lifetime = {
241247
<n:LifetimeId> => Lifetime::Id { name: n },
242248
};

chalk-solve/src/clauses.rs

+1
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ fn match_type_name<I: Interner>(
410410
TypeName::Tuple(_) => {
411411
builder.push_fact(WellFormed::Ty(application.clone().intern(interner)))
412412
}
413+
TypeName::Raw(_) => builder.push_fact(WellFormed::Ty(application.clone().intern(interner))),
413414
}
414415
}
415416

chalk-solve/src/clauses/builtin_traits/copy.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub fn add_copy_program_clauses<I: Interner>(
4141
TypeName::Tuple(arity) => {
4242
push_tuple_copy_conditions(db, builder, trait_ref, *arity, substitution)
4343
}
44+
TypeName::Raw(_) => builder.push_fact(trait_ref.clone()),
4445
_ => return,
4546
},
4647
TyData::Function(_) => builder.push_fact(trait_ref.clone()),

chalk-solve/src/clauses/builtin_traits/sized.rs

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub fn add_sized_program_clauses<I: Interner>(
7575
TypeName::Tuple(arity) => {
7676
push_tuple_sized_conditions(db, builder, trait_ref, *arity, substitution)
7777
}
78+
TypeName::Raw(_) => builder.push_fact(trait_ref.clone()),
7879
_ => return,
7980
},
8081
TyData::Function(_) => builder.push_fact(trait_ref.clone()),

tests/lowering/mod.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,41 @@ fn scalars() {
471471
}
472472

473473
error_msg {
474-
"parse error: UnrecognizedToken { token: (8, Token(49, \"i32\"), 11), expected: [\"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
474+
"parse error: UnrecognizedToken { token: (8, Token(51, \"i32\"), 11), expected: [\"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
475+
}
476+
}
477+
}
478+
479+
#[test]
480+
fn raw_pointers() {
481+
lowering_success! {
482+
program {
483+
trait Quux { }
484+
struct Foo<T> { a: *const T }
485+
486+
struct Bar<T> { a: *mut T }
487+
488+
impl<T> Quux for Foo<*mut T> { }
489+
impl<T> Quux for Bar<*const T> { }
490+
}
491+
}
492+
493+
lowering_error! {
494+
program {
495+
struct *const i32 { }
496+
}
497+
error_msg {
498+
"parse error: UnrecognizedToken { token: (8, Token(7, \"*\"), 9), expected: [\"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
499+
}
500+
}
501+
502+
lowering_error! {
503+
program {
504+
trait Foo { }
505+
impl Foo for *i32 { }
506+
}
507+
error_msg {
508+
"parse error: UnrecognizedToken { token: (30, Token(51, \"i32\"), 33), expected: [\"\\\"const\\\"\", \"\\\"mut\\\"\"] }"
475509
}
476510
}
477511
}

0 commit comments

Comments
 (0)