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 <flodiebold@gmail.com>
This commit is contained in:
bors[bot] 2022-03-23 20:20:09 +00:00 committed by GitHub
commit 75fada4aab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 4 deletions

View file

@ -108,8 +108,13 @@ fn builtin_deref(ty: &Ty) -> Option<&Ty> {
}
fn deref_by_trait(table: &mut InferenceTable, ty: Ty) -> Option<Ty> {
let db = table.db;
let _p = profile::span("deref_by_trait");
if table.resolve_ty_shallow(&ty).inference_var(Interner).is_some() {
// don't try to deref unknown variables
return None;
}
let db = table.db;
let deref_trait = db
.lang_item(table.trait_env.krate, SmolStr::new_inline("deref"))
.and_then(|l| l.as_trait())?;

View file

@ -681,6 +681,11 @@ fn iterate_method_candidates_with_autoref(
name: Option<&Name>,
mut callback: &mut dyn FnMut(ReceiverAdjustments, AssocItemId) -> ControlFlow<()>,
) -> ControlFlow<()> {
if receiver_ty.value.is_general_var(Interner, &receiver_ty.binders) {
// don't try to resolve methods on unknown types
return ControlFlow::Continue(());
}
iterate_method_candidates_by_receiver(
receiver_ty,
first_adjustment.clone(),

View file

@ -2,7 +2,7 @@ use expect_test::expect;
use crate::tests::check;
use super::{check_infer, check_types};
use super::{check_infer, check_no_mismatches, check_types};
#[test]
fn infer_slice_method() {
@ -1697,3 +1697,68 @@ fn test() {
"#,
);
}
#[test]
fn bad_inferred_reference_1() {
check_no_mismatches(
r#"
//- minicore: sized
pub trait Into<T>: Sized {
fn into(self) -> T;
}
impl<T> Into<T> for T {
fn into(self) -> T { self }
}
trait ExactSizeIterator {
fn len(&self) -> usize;
}
pub struct Foo;
impl Foo {
fn len(&self) -> usize { 0 }
}
pub fn test(generic_args: impl Into<Foo>) {
let generic_args = generic_args.into();
generic_args.len();
let _: Foo = generic_args;
}
"#,
);
}
#[test]
fn bad_inferred_reference_2() {
check_no_mismatches(
r#"
//- minicore: deref
trait ExactSizeIterator {
fn len(&self) -> usize;
}
pub struct Foo;
impl Foo {
fn len(&self) -> usize { 0 }
}
pub fn test() {
let generic_args;
generic_args.len();
let _: Foo = generic_args;
}
"#,
);
}
#[test]
fn resolve_minicore_iterator() {
check_types(
r#"
//- minicore: iterators, sized
fn foo() {
let m = core::iter::repeat(()).filter_map(|()| Some(92)).next();
} //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Option<i32>
"#,
);
}

View file

@ -1018,5 +1018,5 @@ fn benchmark_syntax_highlighting_parser() {
.filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Function))
.count()
};
assert_eq!(hash, 1616);
assert_eq!(hash, 1609);
}

View file

@ -518,7 +518,7 @@ pub mod iter {
}
}
}
pub use self::adapters::Take;
pub use self::adapters::{Take, FilterMap};
mod sources {
mod repeat {