Skip to content

Commit 9fb7842

Browse files
committed
Refactor and cleanup parsing/lowering for arrays
1 parent 9dacf1a commit 9fb7842

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

chalk-integration/src/lowering.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -1443,18 +1443,21 @@ impl LowerTy for Ty {
14431443
}
14441444
}
14451445

1446-
trait LowerGenericArg {
1447-
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::GenericArg<ChalkIr>>;
1446+
impl LowerGenericArg for Const {
1447+
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::GenericArg<ChalkIr>> {
1448+
let interner = env.interner();
1449+
match self {
1450+
Const::Id(name) => env.lookup_generic_arg(name),
1451+
Const::WithoutId(const_without_id) => Ok(const_without_id.lower(env)?.cast(interner)),
1452+
}
1453+
}
14481454
}
14491455

1450-
impl LowerGenericArg for GenericArg {
1456+
impl LowerGenericArg for ConstWithoutId {
14511457
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::GenericArg<ChalkIr>> {
14521458
let interner = env.interner();
14531459
match self {
1454-
GenericArg::Ty(ref t) => Ok(t.lower(env)?.cast(interner)),
1455-
GenericArg::Lifetime(ref l) => Ok(l.lower(env)?.cast(interner)),
1456-
GenericArg::Id(name) => env.lookup_generic_arg(&name),
1457-
GenericArg::ConstValue(value) => Ok(chalk_ir::ConstData {
1460+
ConstWithoutId::ConstValue(value) => Ok(chalk_ir::ConstData {
14581461
ty: get_type_of_u32(),
14591462
value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst {
14601463
interned: value.clone(),
@@ -1466,6 +1469,22 @@ impl LowerGenericArg for GenericArg {
14661469
}
14671470
}
14681471

1472+
trait LowerGenericArg {
1473+
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::GenericArg<ChalkIr>>;
1474+
}
1475+
1476+
impl LowerGenericArg for GenericArg {
1477+
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::GenericArg<ChalkIr>> {
1478+
let interner = env.interner();
1479+
match self {
1480+
GenericArg::Ty(ref t) => Ok(t.lower(env)?.cast(interner)),
1481+
GenericArg::Lifetime(ref l) => Ok(l.lower(env)?.cast(interner)),
1482+
GenericArg::Id(name) => env.lookup_generic_arg(&name),
1483+
GenericArg::ConstWithoutId(c) => Ok(c.lower(env)?.cast(interner)),
1484+
}
1485+
}
1486+
}
1487+
14691488
trait LowerLifetime {
14701489
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::Lifetime<ChalkIr>>;
14711490
}

chalk-parse/src/ast.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,20 @@ pub enum GenericArg {
109109
Ty(Ty),
110110
Lifetime(Lifetime),
111111
Id(Identifier),
112+
ConstWithoutId(ConstWithoutId),
113+
}
114+
115+
#[derive(Clone, PartialEq, Eq, Debug)]
116+
pub enum ConstWithoutId {
112117
ConstValue(u32),
113118
}
114119

120+
#[derive(Clone, PartialEq, Eq, Debug)]
121+
pub enum Const {
122+
Id(Identifier),
123+
WithoutId(ConstWithoutId),
124+
}
125+
115126
#[derive(Clone, PartialEq, Eq, Debug)]
116127
/// An inline bound, e.g. `: Foo<K>` in `impl<K, T: Foo<K>> SomeType<T>`.
117128
pub enum InlineBound {
@@ -214,7 +225,7 @@ pub enum Ty {
214225
},
215226
Array {
216227
ty: Box<Ty>,
217-
len: Box<GenericArg>,
228+
len: Const,
218229
},
219230
Raw {
220231
mutability: Mutability,

chalk-parse/src/parser.lalrpop

+11-2
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ TyWithoutId: Ty = {
238238
"&" <l: Lifetime> "mut" <t:Ty> => Ty::Ref{ mutability: Mutability::Mut, lifetime: l, ty: Box::new(t) },
239239
"&" <l: Lifetime> <t:Ty> => Ty::Ref{ mutability: Mutability::Not, lifetime: l, ty: Box::new(t) },
240240
"[" <t:Ty> "]" => Ty::Slice { ty: Box::new(t) },
241-
"[" <t:Ty> ";" <len:GenericArg> "]" => Ty::Array { ty: Box::new(t), len: Box::new(len) },
241+
"[" <t:Ty> ";" <len:Const> "]" => Ty::Array { ty: Box::new(t), len },
242242
};
243243

244244
ScalarType: ScalarType = {
@@ -280,11 +280,20 @@ Lifetime: Lifetime = {
280280
<n:LifetimeId> => Lifetime::Id { name: n },
281281
};
282282

283+
ConstWithoutId: ConstWithoutId = {
284+
ConstValue => ConstWithoutId::ConstValue(<>),
285+
};
286+
287+
Const : Const = {
288+
Id => Const::Id(<>),
289+
ConstWithoutId => Const::WithoutId(<>),
290+
};
291+
283292
GenericArg: GenericArg = {
284293
TyWithoutId => GenericArg::Ty(<>),
285294
Lifetime => GenericArg::Lifetime(<>),
286295
Id => GenericArg::Id(<>),
287-
ConstValue => GenericArg::ConstValue(<>),
296+
ConstWithoutId => GenericArg::ConstWithoutId(<>),
288297
};
289298

290299
ProjectionTy: ProjectionTy = {

tests/lowering/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -595,4 +595,16 @@ fn arrays() {
595595
fn bar<const N>(baz: [Baz; N]);
596596
}
597597
}
598+
599+
lowering_error! {
600+
program {
601+
struct Baz { }
602+
603+
fn foo<T>(baz: [Baz; u32]);
604+
}
605+
606+
error_msg {
607+
"parse error: UnrecognizedToken"
608+
}
609+
}
598610
}

0 commit comments

Comments
 (0)