Skip to content

Commit

Permalink
fix panic on GAT
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Chi <iskyzh@gmail.com>
  • Loading branch information
skyzh committed Apr 11, 2022
1 parent 24cf957 commit 51d6671
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
22 changes: 21 additions & 1 deletion crates/hir_ty/src/tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,7 @@ fn regression_11688_3() {
}

#[test]
fn gat_crash() {
fn gat_crash_1() {
cov_mark::check!(ignore_gats);
check_no_mismatches(
r#"
Expand All @@ -1489,6 +1489,26 @@ fn test<T: Crash>() {
);
}

#[test]
fn gat_crash_2() {
check_no_mismatches(
r#"
pub struct InlineStorage {}
pub struct InlineStorageHandle<T: ?Sized> {}
pub unsafe trait Storage {
type Handle<T: ?Sized>;
fn create<T: ?Sized>() -> Self::Handle<T>;
}
unsafe impl Storage for InlineStorage {
type Handle<T: ?Sized> = InlineStorageHandle<T>;
}
"#,
);
}

#[test]
fn cfgd_out_self_param() {
cov_mark::check!(cfgd_out_self_param);
Expand Down
23 changes: 16 additions & 7 deletions crates/hir_ty/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,22 @@ pub(super) fn associated_type_by_name_including_super_traits(
pub(crate) fn generics(db: &dyn DefDatabase, def: GenericDefId) -> Generics {
let parent_generics = parent_generic_def(db, def).map(|def| Box::new(generics(db, def)));
if parent_generics.is_some() && matches!(def, GenericDefId::TypeAliasId(_)) {
// XXX: treat generic associated types as not existing to avoid crashes (#)
//
// Chalk expects the inner associated type's parameters to come
// *before*, not after the trait's generics as we've always done it.
// Adapting to this requires a larger refactoring
cov_mark::hit!(ignore_gats);
return Generics { def, params: Interned::new(Default::default()), parent_generics };
let params = db.generic_params(def);
if params
.type_or_consts
.iter()
.any(|(_, x)| matches!(x, TypeOrConstParamData::ConstParamData(_)))
{
// XXX: treat const generic associated types as not existing to avoid crashes (#11769)
//
// Chalk expects the inner associated type's parameters to come
// *before*, not after the trait's generics as we've always done it.
// Adapting to this requires a larger refactoring
cov_mark::hit!(ignore_gats);
return Generics { def, params: Interned::new(Default::default()), parent_generics };
} else {
return Generics { def, params, parent_generics };
}
}
Generics { def, params: db.generic_params(def), parent_generics }
}
Expand Down

0 comments on commit 51d6671

Please sign in to comment.