Skip to content

Commit 2ab5116

Browse files
authored
Merge pull request #830 from ChayimFriedman2/no-panic-fmt
fix: Don't panic when formatting without Program
2 parents 4cf6975 + 4343607 commit 2ab5116

File tree

8 files changed

+35
-10
lines changed

8 files changed

+35
-10
lines changed

chalk-integration/src/lowering/program_lowerer.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ impl ProgramLowerer {
152152
let mut closure_upvars = BTreeMap::new();
153153
let mut trait_data = BTreeMap::new();
154154
let mut well_known_traits = BTreeMap::new();
155+
let mut well_known_assoc_types = BTreeMap::new();
155156
let mut impl_data = BTreeMap::new();
156157
let mut associated_ty_data = BTreeMap::new();
157158
let mut associated_ty_values = BTreeMap::new();
@@ -272,6 +273,15 @@ impl ProgramLowerer {
272273
let lookup = &self.associated_ty_lookups
273274
[&(trait_id, assoc_ty_defn.name.str.clone())];
274275

276+
if let Some(well_known) = assoc_ty_defn.well_known {
277+
let well_known = match well_known {
278+
chalk_parse::ast::WellKnownAssocType::AsyncFnOnceOutput => {
279+
chalk_solve::rust_ir::WellKnownAssocType::AsyncFnOnceOutput
280+
}
281+
};
282+
well_known_assoc_types.insert(well_known, lookup.id);
283+
}
284+
275285
// The parameters in scope for the associated
276286
// type definitions are *both* those from the
277287
// trait *and* those from the associated type
@@ -494,6 +504,7 @@ impl ProgramLowerer {
494504
coroutine_witness_data,
495505
trait_data,
496506
well_known_traits,
507+
well_known_assoc_types,
497508
impl_data,
498509
associated_ty_values,
499510
associated_ty_data,

chalk-integration/src/program.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use chalk_ir::{
1111
use chalk_solve::rust_ir::{
1212
AdtDatum, AdtRepr, AdtSizeAlign, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId,
1313
ClosureKind, CoroutineDatum, CoroutineWitnessDatum, FnDefDatum, FnDefInputsAndOutputDatum,
14-
ImplDatum, ImplType, OpaqueTyDatum, TraitDatum, WellKnownTrait,
14+
ImplDatum, ImplType, OpaqueTyDatum, TraitDatum, WellKnownAssocType, WellKnownTrait,
1515
};
1616
use chalk_solve::split::Split;
1717
use chalk_solve::RustIrDatabase;
@@ -96,6 +96,9 @@ pub struct Program {
9696
/// For each trait lang item
9797
pub well_known_traits: BTreeMap<WellKnownTrait, TraitId<ChalkIr>>,
9898

99+
/// For each assoc type lang item
100+
pub well_known_assoc_types: BTreeMap<WellKnownAssocType, AssocTypeId<ChalkIr>>,
101+
99102
/// For each associated ty declaration `type Foo` found in a trait:
100103
pub associated_ty_data: BTreeMap<AssocTypeId<ChalkIr>, Arc<AssociatedTyDatum<ChalkIr>>>,
101104

@@ -539,10 +542,9 @@ impl RustIrDatabase<ChalkIr> for Program {
539542

540543
fn well_known_assoc_type_id(
541544
&self,
542-
_assoc_type: chalk_solve::rust_ir::WellKnownAssocType,
545+
assoc_type: WellKnownAssocType,
543546
) -> Option<AssocTypeId<ChalkIr>> {
544-
// FIXME
545-
None
547+
self.well_known_assoc_types.get(&assoc_type).copied()
546548
}
547549

548550
fn program_clauses_for_env(

chalk-ir/src/debug.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,13 @@ impl<I: Interner> Debug for QuantifiedWhereClauses<I> {
152152

153153
impl<I: Interner> Debug for ProjectionTy<I> {
154154
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
155-
I::debug_projection_ty(self, fmt).unwrap_or_else(|| {
156-
unimplemented!("cannot format ProjectionTy without setting Program in tls")
157-
})
155+
I::debug_projection_ty(self, fmt).unwrap_or_else(|| fmt.write_str("<ProjectionTy>"))
158156
}
159157
}
160158

161159
impl<I: Interner> Debug for OpaqueTy<I> {
162160
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
163-
I::debug_opaque_ty(self, fmt).unwrap_or_else(|| {
164-
unimplemented!("cannot format OpaqueTy without setting Program in tls")
165-
})
161+
I::debug_opaque_ty(self, fmt).unwrap_or_else(|| fmt.write_str("<OpaqueTy>"))
166162
}
167163
}
168164

chalk-parse/src/ast.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ pub struct AssocTyDefn {
187187
pub variable_kinds: Vec<VariableKind>,
188188
pub bounds: Vec<QuantifiedInlineBound>,
189189
pub where_clauses: Vec<QuantifiedWhereClause>,
190+
pub well_known: Option<WellKnownAssocType>,
191+
}
192+
193+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
194+
pub enum WellKnownAssocType {
195+
AsyncFnOnceOutput,
190196
}
191197

192198
#[derive(Clone, PartialEq, Eq, Debug)]

chalk-parse/src/parser.lalrpop

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,12 @@ TraitDefn: TraitDefn = {
283283
}
284284
};
285285

286+
WellKnownAssocType: WellKnownAssocType = {
287+
"#" "[" "lang" "(" "async_fn_once_output" ")" "]" => WellKnownAssocType::AsyncFnOnceOutput,
288+
};
289+
286290
AssocTyDefn: AssocTyDefn = {
291+
<well_known:WellKnownAssocType?>
287292
"type" <name:Id> <p:Angle<VariableKind>> <b:(":" <Plus<QuantifiedInlineBound>>)?>
288293
<w:QuantifiedWhereClauses> ";" =>
289294
{
@@ -292,6 +297,7 @@ AssocTyDefn: AssocTyDefn = {
292297
variable_kinds: p,
293298
where_clauses: w,
294299
bounds: b.unwrap_or(vec![]),
300+
well_known,
295301
}
296302
}
297303
};

tests/test/closures.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ fn closure_implements_fn_traits() {
170170
#[lang(async_fn_once)]
171171
trait AsyncFnOnce<Args> {
172172
type CallOnceFuture: Future<Output = <Self as AsyncFnOnce<Args>>::Output>;
173+
#[lang(async_fn_once_output)]
173174
type Output;
174175
}
175176

tests/test/fn_def.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ fn fn_def_implements_fn_traits() {
8888
#[lang(async_fn_once)]
8989
trait AsyncFnOnce<Args> {
9090
type CallOnceFuture: Future<Output = <Self as AsyncFnOnce<Args>>::Output>;
91+
#[lang(async_fn_once_output)]
9192
type Output;
9293
}
9394

@@ -209,6 +210,7 @@ fn generic_fn_implements_fn_traits() {
209210
#[lang(async_fn_once)]
210211
trait AsyncFnOnce<Args> {
211212
type CallOnceFuture: Future<Output = <Self as AsyncFnOnce<Args>>::Output>;
213+
#[lang(async_fn_once_output)]
212214
type Output;
213215
}
214216

tests/test/functions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ fn function_implement_fn_traits() {
6767
#[lang(async_fn_once)]
6868
trait AsyncFnOnce<Args> {
6969
type CallOnceFuture: Future<Output = <Self as AsyncFnOnce<Args>>::Output>;
70+
#[lang(async_fn_once_output)]
7071
type Output;
7172
}
7273

0 commit comments

Comments
 (0)