Skip to content

Commit 26fe3f9

Browse files
authored
Merge pull request #443 from nathanwhit/typename-slice
Add slice type to `TypeName`
2 parents 582ea06 + 4d440e9 commit 26fe3f9

File tree

10 files changed

+110
-1
lines changed

10 files changed

+110
-1
lines changed

book/src/clauses/well_known_traits.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ Some common examples of auto traits are `Send` and `Sync`.
3636
| trait objects ||||||||||
3737
| functions ptrs ||||||||||
3838
| raw ptrs ||||||||||
39+
| references ||||||||||
40+
| slices ||||||||||
3941
| arrays❌ ||||||||||
40-
| slices❌ ||||||||||
4142
| closures❌ ||||||||||
4243
| generators❌ ||||||||||
4344
| gen. witness❌ ||||||||||

chalk-integration/src/lowering.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,15 @@ impl LowerTy for Ty {
12111211
})
12121212
.intern(interner)),
12131213

1214+
Ty::Slice { ty } => Ok(chalk_ir::TyData::Apply(chalk_ir::ApplicationTy {
1215+
name: chalk_ir::TypeName::Slice,
1216+
substitution: chalk_ir::Substitution::from_fallible(
1217+
interner,
1218+
std::iter::once(ty.lower(env)),
1219+
)?,
1220+
})
1221+
.intern(interner)),
1222+
12141223
Ty::Raw { mutability, ty } => Ok(chalk_ir::TyData::Apply(chalk_ir::ApplicationTy {
12151224
name: chalk_ir::TypeName::Raw(ast_mutability_to_chalk_mutability(
12161225
mutability.clone(),

chalk-ir/src/debug.rs

Lines changed: 1 addition & 0 deletions
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::Slice => write!(fmt, "{{slice}}"),
147148
TypeName::Raw(mutability) => write!(fmt, "{:?}", mutability),
148149
TypeName::Ref(mutability) => write!(fmt, "{:?}", mutability),
149150
TypeName::Error => write!(fmt, "{{error}}"),

chalk-ir/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ pub enum TypeName<I: Interner> {
156156
/// a tuple of the given arity
157157
Tuple(usize),
158158

159+
/// a slice type like `[T]`
160+
Slice,
161+
159162
/// a raw pointer type like `*const T` or `*mut T`
160163
Raw(Mutability),
161164

chalk-parse/src/ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ pub enum Ty {
193193
Scalar {
194194
ty: ScalarType,
195195
},
196+
Slice {
197+
ty: Box<Ty>,
198+
},
196199
Raw {
197200
mutability: Mutability,
198201
ty: Box<Ty>,

chalk-parse/src/parser.lalrpop

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ TyWithoutFor: Ty = {
208208
"*" <m: RawMutability> <t:Ty> => Ty::Raw{ mutability: m, ty: Box::new(t) },
209209
"&" <l: Lifetime> "mut" <t:Ty> => Ty::Ref{ mutability: Mutability::Mut, lifetime: l, ty: Box::new(t) },
210210
"&" <l: Lifetime> <t:Ty> => Ty::Ref{ mutability: Mutability::Not, lifetime: l, ty: Box::new(t) },
211+
"[" <t:Ty> "]" => Ty::Slice { ty: Box::new(t) },
211212
};
212213

213214
ScalarType: ScalarType = {

chalk-solve/src/clauses.rs

Lines changed: 1 addition & 0 deletions
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::Slice => builder.push_fact(WellFormed::Ty(application.clone().intern(interner))),
413414
TypeName::Raw(_) => builder.push_fact(WellFormed::Ty(application.clone().intern(interner))),
414415
TypeName::Ref(_) => builder.push_fact(WellFormed::Ty(application.clone().intern(interner))),
415416
}

tests/lowering/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,29 @@ fn refs() {
533533
}
534534
}
535535
}
536+
537+
#[test]
538+
fn slices() {
539+
lowering_success! {
540+
program {
541+
trait Foo { }
542+
543+
impl Foo for [i32] { }
544+
impl<T> Foo for [T] { }
545+
546+
impl Foo for [[i32]] { }
547+
impl Foo for [()] { }
548+
}
549+
}
550+
551+
lowering_error! {
552+
program {
553+
trait Foo { }
554+
impl Foo for [] {}
555+
}
556+
557+
error_msg {
558+
"parse error: UnrecognizedToken { token: (29, Token(30, \"]\"), 30), expected: [\"\\\"&\\\"\", \"\\\"(\\\"\", \"\\\"*\\\"\", \"\\\"<\\\"\", \"\\\"[\\\"\", \"\\\"bool\\\"\", \"\\\"char\\\"\", \"\\\"dyn\\\"\", \"\\\"f32\\\"\", \"\\\"f64\\\"\", \"\\\"fn\\\"\", \"\\\"for\\\"\", \"\\\"i128\\\"\", \"\\\"i16\\\"\", \"\\\"i32\\\"\", \"\\\"i64\\\"\", \"\\\"i8\\\"\", \"\\\"isize\\\"\", \"\\\"u128\\\"\", \"\\\"u16\\\"\", \"\\\"u32\\\"\", \"\\\"u64\\\"\", \"\\\"u8\\\"\", \"\\\"usize\\\"\", \"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
559+
}
560+
}
561+
}

tests/test/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ mod negation;
316316
mod projection;
317317
mod refs;
318318
mod scalars;
319+
mod slices;
319320
mod tuples;
320321
mod unify;
321322
mod wf_goals;

tests/test/slices.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use super::*;
2+
3+
#[test]
4+
fn slices_are_not_sized() {
5+
test! {
6+
program {
7+
#[lang(sized)]
8+
trait Sized { }
9+
}
10+
11+
goal {
12+
forall<T> { not { [T]: Sized } }
13+
} yields {
14+
"Unique; substitution [], lifetime constraints []"
15+
}
16+
}
17+
}
18+
19+
#[test]
20+
fn slices_are_well_formed() {
21+
test! {
22+
program {
23+
}
24+
25+
goal {
26+
forall<T> { WellFormed([T]) }
27+
} yields {
28+
"Unique; substitution [], lifetime constraints []"
29+
}
30+
}
31+
}
32+
33+
#[test]
34+
fn slices_are_not_copy() {
35+
test! {
36+
program {
37+
#[lang(copy)]
38+
trait Copy { }
39+
}
40+
41+
goal {
42+
forall<T> { not { [T]: Copy } }
43+
} yields {
44+
"Unique; substitution [], lifetime constraints []"
45+
}
46+
}
47+
}
48+
49+
#[test]
50+
fn slices_are_not_clone() {
51+
test! {
52+
program {
53+
#[lang(clone)]
54+
trait Clone { }
55+
}
56+
57+
goal {
58+
forall<T> { not { [T]: Clone } }
59+
} yields {
60+
"Unique; substitution [], lifetime constraints []"
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)