Skip to content

Commit 75fada4

Browse files
Merge #11805
11805: fix: Don't try to resolve methods on unknown types r=Veykril a=flodiebold Fixes #10454, and some type mismatches. Co-authored-by: Florian Diebold <[email protected]>
2 parents de5925d + e0e6bfb commit 75fada4

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

crates/hir_ty/src/autoderef.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ fn builtin_deref(ty: &Ty) -> Option<&Ty> {
108108
}
109109

110110
fn deref_by_trait(table: &mut InferenceTable, ty: Ty) -> Option<Ty> {
111-
let db = table.db;
112111
let _p = profile::span("deref_by_trait");
112+
if table.resolve_ty_shallow(&ty).inference_var(Interner).is_some() {
113+
// don't try to deref unknown variables
114+
return None;
115+
}
116+
117+
let db = table.db;
113118
let deref_trait = db
114119
.lang_item(table.trait_env.krate, SmolStr::new_inline("deref"))
115120
.and_then(|l| l.as_trait())?;

crates/hir_ty/src/method_resolution.rs

+5
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,11 @@ fn iterate_method_candidates_with_autoref(
681681
name: Option<&Name>,
682682
mut callback: &mut dyn FnMut(ReceiverAdjustments, AssocItemId) -> ControlFlow<()>,
683683
) -> ControlFlow<()> {
684+
if receiver_ty.value.is_general_var(Interner, &receiver_ty.binders) {
685+
// don't try to resolve methods on unknown types
686+
return ControlFlow::Continue(());
687+
}
688+
684689
iterate_method_candidates_by_receiver(
685690
receiver_ty,
686691
first_adjustment.clone(),

crates/hir_ty/src/tests/method_resolution.rs

+66-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use expect_test::expect;
22

33
use crate::tests::check;
44

5-
use super::{check_infer, check_types};
5+
use super::{check_infer, check_no_mismatches, check_types};
66

77
#[test]
88
fn infer_slice_method() {
@@ -1697,3 +1697,68 @@ fn test() {
16971697
"#,
16981698
);
16991699
}
1700+
1701+
#[test]
1702+
fn bad_inferred_reference_1() {
1703+
check_no_mismatches(
1704+
r#"
1705+
//- minicore: sized
1706+
pub trait Into<T>: Sized {
1707+
fn into(self) -> T;
1708+
}
1709+
impl<T> Into<T> for T {
1710+
fn into(self) -> T { self }
1711+
}
1712+
1713+
trait ExactSizeIterator {
1714+
fn len(&self) -> usize;
1715+
}
1716+
1717+
pub struct Foo;
1718+
impl Foo {
1719+
fn len(&self) -> usize { 0 }
1720+
}
1721+
1722+
pub fn test(generic_args: impl Into<Foo>) {
1723+
let generic_args = generic_args.into();
1724+
generic_args.len();
1725+
let _: Foo = generic_args;
1726+
}
1727+
"#,
1728+
);
1729+
}
1730+
1731+
#[test]
1732+
fn bad_inferred_reference_2() {
1733+
check_no_mismatches(
1734+
r#"
1735+
//- minicore: deref
1736+
trait ExactSizeIterator {
1737+
fn len(&self) -> usize;
1738+
}
1739+
1740+
pub struct Foo;
1741+
impl Foo {
1742+
fn len(&self) -> usize { 0 }
1743+
}
1744+
1745+
pub fn test() {
1746+
let generic_args;
1747+
generic_args.len();
1748+
let _: Foo = generic_args;
1749+
}
1750+
"#,
1751+
);
1752+
}
1753+
1754+
#[test]
1755+
fn resolve_minicore_iterator() {
1756+
check_types(
1757+
r#"
1758+
//- minicore: iterators, sized
1759+
fn foo() {
1760+
let m = core::iter::repeat(()).filter_map(|()| Some(92)).next();
1761+
} //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Option<i32>
1762+
"#,
1763+
);
1764+
}

crates/ide/src/syntax_highlighting/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1018,5 +1018,5 @@ fn benchmark_syntax_highlighting_parser() {
10181018
.filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Function))
10191019
.count()
10201020
};
1021-
assert_eq!(hash, 1616);
1021+
assert_eq!(hash, 1609);
10221022
}

crates/test_utils/src/minicore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ pub mod iter {
518518
}
519519
}
520520
}
521-
pub use self::adapters::Take;
521+
pub use self::adapters::{Take, FilterMap};
522522

523523
mod sources {
524524
mod repeat {

0 commit comments

Comments
 (0)