Skip to content

fix: Don't panic when formatting without Program #830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions chalk-integration/src/lowering/program_lowerer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl ProgramLowerer {
let mut closure_upvars = BTreeMap::new();
let mut trait_data = BTreeMap::new();
let mut well_known_traits = BTreeMap::new();
let mut well_known_assoc_types = BTreeMap::new();
let mut impl_data = BTreeMap::new();
let mut associated_ty_data = BTreeMap::new();
let mut associated_ty_values = BTreeMap::new();
Expand Down Expand Up @@ -272,6 +273,15 @@ impl ProgramLowerer {
let lookup = &self.associated_ty_lookups
[&(trait_id, assoc_ty_defn.name.str.clone())];

if let Some(well_known) = assoc_ty_defn.well_known {
let well_known = match well_known {
chalk_parse::ast::WellKnownAssocType::AsyncFnOnceOutput => {
chalk_solve::rust_ir::WellKnownAssocType::AsyncFnOnceOutput
}
};
well_known_assoc_types.insert(well_known, lookup.id);
}

// The parameters in scope for the associated
// type definitions are *both* those from the
// trait *and* those from the associated type
Expand Down Expand Up @@ -494,6 +504,7 @@ impl ProgramLowerer {
coroutine_witness_data,
trait_data,
well_known_traits,
well_known_assoc_types,
impl_data,
associated_ty_values,
associated_ty_data,
Expand Down
10 changes: 6 additions & 4 deletions chalk-integration/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use chalk_ir::{
use chalk_solve::rust_ir::{
AdtDatum, AdtRepr, AdtSizeAlign, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId,
ClosureKind, CoroutineDatum, CoroutineWitnessDatum, FnDefDatum, FnDefInputsAndOutputDatum,
ImplDatum, ImplType, OpaqueTyDatum, TraitDatum, WellKnownTrait,
ImplDatum, ImplType, OpaqueTyDatum, TraitDatum, WellKnownAssocType, WellKnownTrait,
};
use chalk_solve::split::Split;
use chalk_solve::RustIrDatabase;
Expand Down Expand Up @@ -96,6 +96,9 @@ pub struct Program {
/// For each trait lang item
pub well_known_traits: BTreeMap<WellKnownTrait, TraitId<ChalkIr>>,

/// For each assoc type lang item
pub well_known_assoc_types: BTreeMap<WellKnownAssocType, AssocTypeId<ChalkIr>>,

/// For each associated ty declaration `type Foo` found in a trait:
pub associated_ty_data: BTreeMap<AssocTypeId<ChalkIr>, Arc<AssociatedTyDatum<ChalkIr>>>,

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

fn well_known_assoc_type_id(
&self,
_assoc_type: chalk_solve::rust_ir::WellKnownAssocType,
assoc_type: WellKnownAssocType,
) -> Option<AssocTypeId<ChalkIr>> {
// FIXME
None
self.well_known_assoc_types.get(&assoc_type).copied()
}

fn program_clauses_for_env(
Expand Down
8 changes: 2 additions & 6 deletions chalk-ir/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,13 @@ impl<I: Interner> Debug for QuantifiedWhereClauses<I> {

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

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

Expand Down
6 changes: 6 additions & 0 deletions chalk-parse/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ pub struct AssocTyDefn {
pub variable_kinds: Vec<VariableKind>,
pub bounds: Vec<QuantifiedInlineBound>,
pub where_clauses: Vec<QuantifiedWhereClause>,
pub well_known: Option<WellKnownAssocType>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum WellKnownAssocType {
AsyncFnOnceOutput,
}

#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down
6 changes: 6 additions & 0 deletions chalk-parse/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ TraitDefn: TraitDefn = {
}
};

WellKnownAssocType: WellKnownAssocType = {
"#" "[" "lang" "(" "async_fn_once_output" ")" "]" => WellKnownAssocType::AsyncFnOnceOutput,
};

AssocTyDefn: AssocTyDefn = {
<well_known:WellKnownAssocType?>
"type" <name:Id> <p:Angle<VariableKind>> <b:(":" <Plus<QuantifiedInlineBound>>)?>
<w:QuantifiedWhereClauses> ";" =>
{
Expand All @@ -292,6 +297,7 @@ AssocTyDefn: AssocTyDefn = {
variable_kinds: p,
where_clauses: w,
bounds: b.unwrap_or(vec![]),
well_known,
}
}
};
Expand Down
1 change: 1 addition & 0 deletions tests/test/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ fn closure_implements_fn_traits() {
#[lang(async_fn_once)]
trait AsyncFnOnce<Args> {
type CallOnceFuture: Future<Output = <Self as AsyncFnOnce<Args>>::Output>;
#[lang(async_fn_once_output)]
type Output;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/test/fn_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn fn_def_implements_fn_traits() {
#[lang(async_fn_once)]
trait AsyncFnOnce<Args> {
type CallOnceFuture: Future<Output = <Self as AsyncFnOnce<Args>>::Output>;
#[lang(async_fn_once_output)]
type Output;
}

Expand Down Expand Up @@ -209,6 +210,7 @@ fn generic_fn_implements_fn_traits() {
#[lang(async_fn_once)]
trait AsyncFnOnce<Args> {
type CallOnceFuture: Future<Output = <Self as AsyncFnOnce<Args>>::Output>;
#[lang(async_fn_once_output)]
type Output;
}

Expand Down
1 change: 1 addition & 0 deletions tests/test/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fn function_implement_fn_traits() {
#[lang(async_fn_once)]
trait AsyncFnOnce<Args> {
type CallOnceFuture: Future<Output = <Self as AsyncFnOnce<Args>>::Output>;
#[lang(async_fn_once_output)]
type Output;
}

Expand Down
Loading