Skip to content

Commit 176fd2b

Browse files
committed
Refactor parsing/lowering of consts
For use in parsing array types
1 parent 4269646 commit 176fd2b

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

chalk-integration/src/lowering.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,36 @@ impl LowerTy for Ty {
14601460
}
14611461
}
14621462

1463+
trait LowerConst {
1464+
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::Const<ChalkIr>>;
1465+
}
1466+
1467+
impl LowerConst for Const {
1468+
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::Const<ChalkIr>> {
1469+
let interner = env.interner();
1470+
match self {
1471+
Const::Id(name) => {
1472+
let parameter = env.lookup_generic_arg(name)?;
1473+
parameter
1474+
.constant(interner)
1475+
.ok_or_else(|| RustIrError::IncorrectParameterKind {
1476+
identifier: name.clone(),
1477+
expected: Kind::Const,
1478+
actual: parameter.kind(),
1479+
})
1480+
.map(|c| c.clone())
1481+
}
1482+
Const::Value(value) => Ok(chalk_ir::ConstData {
1483+
ty: get_type_of_u32(),
1484+
value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst {
1485+
interned: value.clone(),
1486+
}),
1487+
}
1488+
.intern(interner)),
1489+
}
1490+
}
1491+
}
1492+
14631493
trait LowerGenericArg {
14641494
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::GenericArg<ChalkIr>>;
14651495
}
@@ -1471,14 +1501,7 @@ impl LowerGenericArg for GenericArg {
14711501
GenericArg::Ty(ref t) => Ok(t.lower(env)?.cast(interner)),
14721502
GenericArg::Lifetime(ref l) => Ok(l.lower(env)?.cast(interner)),
14731503
GenericArg::Id(name) => env.lookup_generic_arg(&name),
1474-
GenericArg::ConstValue(value) => Ok(chalk_ir::ConstData {
1475-
ty: get_type_of_u32(),
1476-
value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst {
1477-
interned: value.clone(),
1478-
}),
1479-
}
1480-
.intern(interner)
1481-
.cast(interner)),
1504+
GenericArg::Const(c) => Ok(c.lower(env)?.cast(interner)),
14821505
}
14831506
}
14841507
}

chalk-parse/src/ast.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,13 @@ pub enum GenericArg {
109109
Ty(Ty),
110110
Lifetime(Lifetime),
111111
Id(Identifier),
112-
ConstValue(u32),
112+
Const(Const),
113+
}
114+
115+
#[derive(Clone, PartialEq, Eq, Debug)]
116+
pub enum Const {
117+
Id(Identifier),
118+
Value(u32),
113119
}
114120

115121
#[derive(Clone, PartialEq, Eq, Debug)]
@@ -214,7 +220,7 @@ pub enum Ty {
214220
},
215221
Array {
216222
ty: Box<Ty>,
217-
len: Box<GenericArg>,
223+
len: Const,
218224
},
219225
Raw {
220226
mutability: Mutability,

chalk-parse/src/parser.lalrpop

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

245245
ScalarType: ScalarType = {
@@ -281,11 +281,20 @@ Lifetime: Lifetime = {
281281
<n:LifetimeId> => Lifetime::Id { name: n },
282282
};
283283

284+
ConstWithoutId: Const = {
285+
ConstValue => Const::Value(<>),
286+
};
287+
288+
Const : Const = {
289+
Id => Const::Id(<>),
290+
ConstWithoutId,
291+
};
292+
284293
GenericArg: GenericArg = {
285294
TyWithoutId => GenericArg::Ty(<>),
286295
Lifetime => GenericArg::Lifetime(<>),
287296
Id => GenericArg::Id(<>),
288-
ConstValue => GenericArg::ConstValue(<>),
297+
ConstWithoutId => GenericArg::Const(<>),
289298
};
290299

291300
ProjectionTy: ProjectionTy = {

0 commit comments

Comments
 (0)