Skip to content

Commit 9f84704

Browse files
committed
Add one ZST flag to ADTs
Signed-off-by: Nick Cameron <nrc@ncameron.org>
1 parent 7a90a0a commit 9f84704

File tree

12 files changed

+68
-9
lines changed

12 files changed

+68
-9
lines changed

chalk-integration/src/db.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use chalk_ir::{
1313
UnificationDatabase, Variances,
1414
};
1515
use chalk_solve::rust_ir::{
16-
AdtDatum, AdtRepr, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ClosureKind,
17-
FnDefDatum, FnDefInputsAndOutputDatum, GeneratorDatum, GeneratorWitnessDatum, ImplDatum,
18-
OpaqueTyDatum, TraitDatum, WellKnownTrait,
16+
AdtDatum, AdtRepr, AdtSizeAlign, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId,
17+
ClosureKind, FnDefDatum, FnDefInputsAndOutputDatum, GeneratorDatum, GeneratorWitnessDatum,
18+
ImplDatum, OpaqueTyDatum, TraitDatum, WellKnownTrait,
1919
};
2020
use chalk_solve::{RustIrDatabase, Solution, SubstitutionResult};
2121
use salsa::Database;
@@ -133,6 +133,10 @@ impl RustIrDatabase<ChalkIr> for ChalkDatabase {
133133
self.program_ir().unwrap().adt_repr(id)
134134
}
135135

136+
fn adt_size_align(&self, id: AdtId<ChalkIr>) -> Arc<AdtSizeAlign> {
137+
self.program_ir().unwrap().adt_size_align(id)
138+
}
139+
136140
fn fn_def_datum(&self, id: FnDefId<ChalkIr>) -> Arc<FnDefDatum<ChalkIr>> {
137141
self.program_ir().unwrap().fn_def_datum(id)
138142
}

chalk-integration/src/lowering.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ impl LowerWithEnv for (&AdtDefn, chalk_ir::AdtId<ChalkIr>) {
325325
}
326326
}
327327

328+
pub fn lower_adt_size_align(flags: &AdtFlags) -> rust_ir::AdtSizeAlign {
329+
rust_ir::AdtSizeAlign::from_one_zst(flags.one_zst)
330+
}
331+
328332
impl LowerWithEnv for AdtRepr {
329333
type Lowered = rust_ir::AdtRepr<ChalkIr>;
330334

chalk-integration/src/lowering/program_lowerer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::collections::{BTreeMap, HashSet};
1313
use std::sync::Arc;
1414
use string_cache::DefaultAtom as Atom;
1515

16-
use super::{env::*, Lower, LowerParameterMap, LowerWithEnv, FIXME_SELF};
16+
use super::{env::*, lower_adt_size_align, Lower, LowerParameterMap, LowerWithEnv, FIXME_SELF};
1717
use crate::error::RustIrError;
1818
use crate::program::Program as LoweredProgram;
1919
use crate::RawId;
@@ -143,6 +143,7 @@ impl ProgramLowerer {
143143
pub fn lower(self, program: &Program, raw_ids: &[RawId]) -> LowerResult<LoweredProgram> {
144144
let mut adt_data = BTreeMap::new();
145145
let mut adt_reprs = BTreeMap::new();
146+
let mut adt_size_aligns = BTreeMap::new();
146147
let mut adt_variances = BTreeMap::new();
147148
let mut fn_def_data = BTreeMap::new();
148149
let mut fn_def_variances = BTreeMap::new();
@@ -186,6 +187,7 @@ impl ProgramLowerer {
186187
let adt_id = AdtId(raw_id);
187188
adt_data.insert(adt_id, Arc::new((d, adt_id).lower(&empty_env)?));
188189
adt_reprs.insert(adt_id, Arc::new(d.repr.lower(&empty_env)?));
190+
adt_size_aligns.insert(adt_id, Arc::new(lower_adt_size_align(&d.flags)));
189191
let n_params = d.all_parameters().len();
190192
let variances = match d.variances.clone() {
191193
Some(v) => {
@@ -480,6 +482,7 @@ impl ProgramLowerer {
480482
trait_kinds: self.trait_kinds,
481483
adt_data,
482484
adt_reprs,
485+
adt_size_aligns,
483486
adt_variances,
484487
fn_def_data,
485488
fn_def_variances,

chalk-integration/src/program.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use chalk_ir::{
99
Substitution, TraitId, Ty, TyKind, UintTy, Variances,
1010
};
1111
use chalk_solve::rust_ir::{
12-
AdtDatum, AdtRepr, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ClosureKind,
13-
FnDefDatum, FnDefInputsAndOutputDatum, GeneratorDatum, GeneratorWitnessDatum, ImplDatum,
14-
ImplType, OpaqueTyDatum, TraitDatum, WellKnownTrait,
12+
AdtDatum, AdtRepr, AdtSizeAlign, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId,
13+
ClosureKind, FnDefDatum, FnDefInputsAndOutputDatum, GeneratorDatum, GeneratorWitnessDatum,
14+
ImplDatum, ImplType, OpaqueTyDatum, TraitDatum, WellKnownTrait,
1515
};
1616
use chalk_solve::split::Split;
1717
use chalk_solve::RustIrDatabase;
@@ -61,6 +61,8 @@ pub struct Program {
6161

6262
pub adt_reprs: BTreeMap<AdtId<ChalkIr>, Arc<AdtRepr<ChalkIr>>>,
6363

64+
pub adt_size_aligns: BTreeMap<AdtId<ChalkIr>, Arc<AdtSizeAlign>>,
65+
6466
pub fn_def_data: BTreeMap<FnDefId<ChalkIr>, Arc<FnDefDatum<ChalkIr>>>,
6567

6668
pub closure_inputs_and_output:
@@ -430,6 +432,10 @@ impl RustIrDatabase<ChalkIr> for Program {
430432
self.adt_reprs[&id].clone()
431433
}
432434

435+
fn adt_size_align(&self, id: AdtId<ChalkIr>) -> Arc<AdtSizeAlign> {
436+
self.adt_size_aligns[&id].clone()
437+
}
438+
433439
fn fn_def_datum(&self, id: FnDefId<ChalkIr>) -> Arc<FnDefDatum<ChalkIr>> {
434440
self.fn_def_data[&id].clone()
435441
}

chalk-parse/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub struct AdtFlags {
7474
pub upstream: bool,
7575
pub fundamental: bool,
7676
pub phantom_data: bool,
77+
pub one_zst: bool,
7778
pub kind: AdtKind,
7879
}
7980

chalk-parse/src/parser.lalrpop

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ NonEnumerableKeyword: () = "#" "[" "non_enumerable" "]";
5353
CoinductiveKeyword: () = "#" "[" "coinductive" "]";
5454
ObjectSafeKeyword: () = "#" "[" "object_safe" "]";
5555
PhantomDataKeyword: () = "#" "[" "phantom_data" "]";
56+
OneZstKeyword: () = "#" "[" "one_zst" "]";
5657

5758
WellKnownTrait: WellKnownTrait = {
5859
"#" "[" "lang" "(" "sized" ")" "]" => WellKnownTrait::Sized,
@@ -91,7 +92,7 @@ ReprIntTy: Ty = {
9192
}
9293

9394
AdtDefn: AdtDefn = {
94-
<variances:Variances?> <upstream:UpstreamKeyword?> <fundamental:FundamentalKeyword?> <phantom_data:PhantomDataKeyword?> <repr:AdtReprAttr*>
95+
<variances:Variances?> <upstream:UpstreamKeyword?> <fundamental:FundamentalKeyword?> <phantom_data:PhantomDataKeyword?> <one_zst:OneZstKeyword?> <repr:AdtReprAttr*>
9596
"enum" <n:Id><p:Angle<VariableKind>>
9697
<w:QuantifiedWhereClauses> "{" <v:Variants> "}" => AdtDefn
9798
{
@@ -103,6 +104,7 @@ AdtDefn: AdtDefn = {
103104
upstream: upstream.is_some(),
104105
fundamental: fundamental.is_some(),
105106
phantom_data: phantom_data.is_some(),
107+
one_zst: one_zst.is_some(),
106108
kind: AdtKind::Enum,
107109
},
108110
repr: AdtRepr {
@@ -116,7 +118,7 @@ AdtDefn: AdtDefn = {
116118
},
117119
variances,
118120
},
119-
<variances:Variances?> <upstream:UpstreamKeyword?> <fundamental:FundamentalKeyword?> <phantom_data:PhantomDataKeyword?> <repr:AdtReprAttr*>
121+
<variances:Variances?> <upstream:UpstreamKeyword?> <fundamental:FundamentalKeyword?> <phantom_data:PhantomDataKeyword?> <one_zst:OneZstKeyword?> <repr:AdtReprAttr*>
120122
"struct" <n:Id><p:Angle<VariableKind>>
121123
<w:QuantifiedWhereClauses> "{" <f:Fields> "}" => AdtDefn
122124
{
@@ -135,6 +137,7 @@ AdtDefn: AdtDefn = {
135137
upstream: upstream.is_some(),
136138
fundamental: fundamental.is_some(),
137139
phantom_data: phantom_data.is_some(),
140+
one_zst: one_zst.is_some(),
138141
kind: AdtKind::Struct,
139142
},
140143
repr: AdtRepr {

chalk-solve/src/display/stub.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ impl<I: Interner, DB: RustIrDatabase<I>> RustIrDatabase<I> for StubWrapper<'_, D
9090
self.db.adt_repr(id)
9191
}
9292

93+
fn adt_size_align(&self, id: chalk_ir::AdtId<I>) -> Arc<crate::rust_ir::AdtSizeAlign> {
94+
self.db.adt_size_align(id)
95+
}
96+
9397
fn fn_def_datum(
9498
&self,
9599
fn_def_id: chalk_ir::FnDefId<I>,

chalk-solve/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ pub trait RustIrDatabase<I: Interner>: Debug {
6767
/// Returns the representation for the ADT definition with the given id.
6868
fn adt_repr(&self, id: AdtId<I>) -> Arc<AdtRepr<I>>;
6969

70+
fn adt_size_align(&self, id: AdtId<I>) -> Arc<AdtSizeAlign>;
71+
7072
/// Returns the datum for the fn definition with the given id.
7173
fn fn_def_datum(&self, fn_def_id: FnDefId<I>) -> Arc<FnDefDatum<I>>;
7274

chalk-solve/src/logging_db.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ where
156156
self.ws.db().adt_repr(id)
157157
}
158158

159+
fn adt_size_align(&self, id: chalk_ir::AdtId<I>) -> Arc<crate::rust_ir::AdtSizeAlign> {
160+
self.record(id);
161+
self.ws.db().adt_size_align(id)
162+
}
163+
159164
fn impl_datum(&self, impl_id: ImplId<I>) -> Arc<ImplDatum<I>> {
160165
self.record(impl_id);
161166
self.ws.db().impl_datum(impl_id)
@@ -421,6 +426,10 @@ where
421426
self.db.adt_repr(id)
422427
}
423428

429+
fn adt_size_align(&self, id: chalk_ir::AdtId<I>) -> Arc<crate::rust_ir::AdtSizeAlign> {
430+
self.db.adt_size_align(id)
431+
}
432+
424433
fn impl_datum(&self, impl_id: ImplId<I>) -> Arc<ImplDatum<I>> {
425434
self.db.impl_datum(impl_id)
426435
}

chalk-solve/src/rust_ir.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ pub struct AdtRepr<I: Interner> {
121121
pub int: Option<chalk_ir::Ty<I>>,
122122
}
123123

124+
/// Information about the size and alignment of an ADT.
125+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
126+
pub struct AdtSizeAlign {
127+
one_zst: bool,
128+
}
129+
130+
impl AdtSizeAlign {
131+
pub fn from_one_zst(one_zst: bool) -> AdtSizeAlign {
132+
AdtSizeAlign { one_zst }
133+
}
134+
135+
pub fn one_zst(&self) -> bool {
136+
self.one_zst
137+
}
138+
}
139+
124140
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
125141
/// A rust intermediate represention (rust_ir) of a function definition/declaration.
126142
/// For example, in the following rust code:

0 commit comments

Comments
 (0)