Skip to content

Commit

Permalink
Auto merge of #17891 - lnicola:binop-bad-lang-items, r=flodiebold
Browse files Browse the repository at this point in the history
internal: Be more resilient to bad language item definitions in binop inference

Fixes #16287
Fixes #16286

There's one more in `write_fn_trait_method_resolution`, but I'm not sure if it won't cause further problems in `infer_closures`.
  • Loading branch information
bors committed Aug 14, 2024
2 parents b6913c5 + e37df4c commit 36a071c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
21 changes: 12 additions & 9 deletions crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,11 +793,12 @@ impl InferenceContext<'_> {
.trait_data(index_trait)
.method_by_name(&Name::new_symbol_root(sym::index.clone()))
{
let substs = TyBuilder::subst_for_def(self.db, index_trait, None)
.push(self_ty.clone())
.push(index_ty.clone())
.build();
self.write_method_resolution(tgt_expr, func, substs);
let subst = TyBuilder::subst_for_def(self.db, index_trait, None);
if subst.remaining() != 2 {
return self.err_ty();
}
let subst = subst.push(self_ty.clone()).push(index_ty.clone()).build();
self.write_method_resolution(tgt_expr, func, subst);
}
let assoc = self.resolve_ops_index_output();
let res = self.resolve_associated_type_with_params(
Expand Down Expand Up @@ -1295,10 +1296,12 @@ impl InferenceContext<'_> {

// HACK: We can use this substitution for the function because the function itself doesn't
// have its own generic parameters.
let subst = TyBuilder::subst_for_def(self.db, trait_, None)
.push(lhs_ty.clone())
.push(rhs_ty.clone())
.build();
let subst = TyBuilder::subst_for_def(self.db, trait_, None);
if subst.remaining() != 2 {
return Ty::new(Interner, TyKind::Error);
}
let subst = subst.push(lhs_ty.clone()).push(rhs_ty.clone()).build();

self.write_method_resolution(tgt_expr, func, subst.clone());

let method_ty = self.db.value_ty(func.into()).unwrap().substitute(Interner, &subst);
Expand Down
33 changes: 33 additions & 0 deletions crates/hir-ty/src/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3686,3 +3686,36 @@ fn main() {
"#,
);
}

#[test]
fn infer_bad_lang_item() {
check_infer(
r#"
#[lang="eq"]
pub trait Eq {
fn eq(&self, ) -> bool;
}
#[lang="shr"]
pub trait Shr<RHS,Result> {
fn shr(&self, rhs: &RHS) -> Result;
}
fn test() -> bool {
1 >> 1;
1 == 1;
}
"#,
expect![[r#"
39..43 'self': &'? Self
114..118 'self': &'? Self
120..123 'rhs': &'? RHS
163..190 '{ ...= 1; }': bool
169..170 '1': i32
169..175 '1 >> 1': {unknown}
181..182 '1': i32
181..187 '1 == 1': {unknown}
"#]],
);
}

0 comments on commit 36a071c

Please sign in to comment.