Skip to content

Commit

Permalink
fix: rust-lang#12247 fully qualified call required to determine trait…
Browse files Browse the repository at this point in the history
… method type
  • Loading branch information
bitgaoshu committed Feb 4, 2023
1 parent 3bc33c7 commit 53e24fe
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/hir-expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,13 @@ pub mod known {
IntoIter,
Try,
Ok,
Err,
Future,
IntoFuture,
Result,
Option,
Output,
Residual,
Target,
Box,
RangeFrom,
Expand Down
5 changes: 5 additions & 0 deletions crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,11 @@ impl<'a> InferenceContext<'a> {
self.resolve_output_on(self.resolve_lang_trait(LangItem::Try)?)
}

fn resolve_ops_try_err(&self) -> Option<TypeAliasId> {
let trait_ = self.resolve_lang_trait(LangItem::Try)?;
self.db.trait_data(trait_).associated_type_by_name(&name![Residual])
}

fn resolve_ops_neg_output(&self) -> Option<TypeAliasId> {
self.resolve_output_on(self.resolve_lang_trait(LangItem::Neg)?)
}
Expand Down
1 change: 1 addition & 0 deletions crates/hir-ty/src/infer/coerce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ impl<'a> InferenceTable<'a> {
}
_ => {
// Otherwise, just use unification rules.
eprintln!("just unification rules");
self.unify_and(&from_ty, to_ty, identity)
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,14 @@ impl<'a> InferenceContext<'a> {
.build();
self.write_method_resolution(tgt_expr, func, subst.clone());
}
let actual_ret = inner_ty.clone();
let residual = self.resolve_ops_try_err();
let expect_err = self.resolve_associated_type(self.return_ty.clone(), residual);
let actual_err = self.resolve_associated_type(actual_ret, residual);
match self.table.coerce(&actual_err, &expect_err) {
Err(_) => (),
Ok(_) => (),
}
let try_output = self.resolve_output_on(trait_);
self.resolve_associated_type(inner_ty, try_output)
} else {
Expand Down
85 changes: 85 additions & 0 deletions crates/hir-ty/src/tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,91 @@ fn clone_iter<T>(s: Iter<T>) {
)
}

#[test]
fn issue_12247() {
check_infer(
r#"
//- minicore: result, try
fn aa() -> Result<i32, E3> {
let a : Result<i32, E1> = Err(E1);
let b = a.mapE()?;
}
struct E1;
struct E2;
struct E3;
impl From<E1> for E2 {
fn from(value: E1) -> Self {
E2
}
}
impl From<E1> for E3 {
fn from(value: E1) -> Self {
E3
}
}
trait MapErr<T, E> {
fn mapE(self) -> Result<T, E>;
}
impl <T, ET> MapErr<T, E2> for Result<T, ET>
where
ET: Into<E2>,
{
fn mapE(self) -> Result<T, E2> {
self.map_err(|e| e.into())
}
}
impl <T, ET> MapErr<T, E3> for Result<T, ET>
where
ET: Into<E3>,
{
fn mapE(self) -> Result<T, E3> {
self.map_err(|e| e.into())
}
}
"#, expect![
r#"
27..92 '{ ...()?; }': Result<i32, E3>
37..38 'a': Result<i32, E1>
59..62 'Err': Err<i32, E1>(E1) -> Result<i32, E1>
59..66 'Err(E1)': Result<i32, E1>
63..65 'E1': E1
76..77 'b': Try::Output<Result<i32, E3>>
80..81 'a': Result<i32, E1>
80..88 'a.mapE()': Result<i32, E3>
80..89 'a.mapE()?': Try::Output<Result<i32, E3>>
162..167 'value': E1
181..199 '{ ... }': E2
191..193 'E2': E2
238..243 'value': E1
257..275 '{ ... }': E3
267..269 'E3': E3
311..315 'self': Self
420..424 'self': Result<T, ET>
443..485 '{ ... }': Result<T, E2>
453..457 'self': Result<T, ET>
453..479 'self.m...nto())': Result<T, E2>
466..478 '|e| e.into()': |{unknown}| -> {unknown}
467..468 'e': {unknown}
470..471 'e': {unknown}
470..478 'e.into()': {unknown}
572..576 'self': Result<T, ET>
595..637 '{ ... }': Result<T, E3>
605..609 'self': Result<T, ET>
605..631 'self.m...nto())': Result<T, E3>
618..630 '|e| e.into()': |{unknown}| -> {unknown}
619..620 'e': {unknown}
622..623 'e': {unknown}
622..630 'e.into()': {unknown}
"#
]
)
}


#[test]
fn issue_8686() {
check_infer(
Expand Down

0 comments on commit 53e24fe

Please sign in to comment.