Skip to content

Commit e2f2e73

Browse files
committed
Auto merge of rust-lang#17891 - lnicola:binop-bad-lang-items, r=flodiebold
internal: Be more resilient to bad language item definitions in binop inference Fixes rust-lang#16287 Fixes rust-lang#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`.
2 parents 54ecca0 + 4a14155 commit e2f2e73

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/infer/expr.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -793,11 +793,12 @@ impl InferenceContext<'_> {
793793
.trait_data(index_trait)
794794
.method_by_name(&Name::new_symbol_root(sym::index.clone()))
795795
{
796-
let substs = TyBuilder::subst_for_def(self.db, index_trait, None)
797-
.push(self_ty.clone())
798-
.push(index_ty.clone())
799-
.build();
800-
self.write_method_resolution(tgt_expr, func, substs);
796+
let subst = TyBuilder::subst_for_def(self.db, index_trait, None);
797+
if subst.remaining() != 2 {
798+
return self.err_ty();
799+
}
800+
let subst = subst.push(self_ty.clone()).push(index_ty.clone()).build();
801+
self.write_method_resolution(tgt_expr, func, subst);
801802
}
802803
let assoc = self.resolve_ops_index_output();
803804
let res = self.resolve_associated_type_with_params(
@@ -1295,10 +1296,12 @@ impl InferenceContext<'_> {
12951296

12961297
// HACK: We can use this substitution for the function because the function itself doesn't
12971298
// have its own generic parameters.
1298-
let subst = TyBuilder::subst_for_def(self.db, trait_, None)
1299-
.push(lhs_ty.clone())
1300-
.push(rhs_ty.clone())
1301-
.build();
1299+
let subst = TyBuilder::subst_for_def(self.db, trait_, None);
1300+
if subst.remaining() != 2 {
1301+
return Ty::new(Interner, TyKind::Error);
1302+
}
1303+
let subst = subst.push(lhs_ty.clone()).push(rhs_ty.clone()).build();
1304+
13021305
self.write_method_resolution(tgt_expr, func, subst.clone());
13031306

13041307
let method_ty = self.db.value_ty(func.into()).unwrap().substitute(Interner, &subst);

src/tools/rust-analyzer/crates/hir-ty/src/tests/simple.rs

+33
Original file line numberDiff line numberDiff line change
@@ -3686,3 +3686,36 @@ fn main() {
36863686
"#,
36873687
);
36883688
}
3689+
3690+
#[test]
3691+
fn infer_bad_lang_item() {
3692+
check_infer(
3693+
r#"
3694+
#[lang="eq"]
3695+
pub trait Eq {
3696+
fn eq(&self, ) -> bool;
3697+
3698+
}
3699+
3700+
#[lang="shr"]
3701+
pub trait Shr<RHS,Result> {
3702+
fn shr(&self, rhs: &RHS) -> Result;
3703+
}
3704+
3705+
fn test() -> bool {
3706+
1 >> 1;
3707+
1 == 1;
3708+
}
3709+
"#,
3710+
expect![[r#"
3711+
39..43 'self': &'? Self
3712+
114..118 'self': &'? Self
3713+
120..123 'rhs': &'? RHS
3714+
163..190 '{ ...= 1; }': bool
3715+
169..170 '1': i32
3716+
169..175 '1 >> 1': {unknown}
3717+
181..182 '1': i32
3718+
181..187 '1 == 1': {unknown}
3719+
"#]],
3720+
);
3721+
}

0 commit comments

Comments
 (0)