Don't emit arg count diagnostics for method calls with unknown receiver

Fixes #7098.
This commit is contained in:
Florian Diebold 2021-01-01 20:45:49 +01:00
parent c92c9fdc52
commit 29acd39800
2 changed files with 29 additions and 1 deletions

View File

@ -156,7 +156,7 @@ fn validate_call(&mut self, db: &dyn HirDatabase, call_id: ExprId, expr: &Expr)
// FIXME: Due to shortcomings in the current type system implementation, only emit this
// diagnostic if there are no type mismatches in the containing function.
if self.infer.type_mismatches.iter().next().is_some() {
return Some(());
return None;
}
let is_method_call = matches!(expr, Expr::MethodCall { .. });
@ -170,6 +170,14 @@ fn validate_call(&mut self, db: &dyn HirDatabase, call_id: ExprId, expr: &Expr)
let mut args = args.clone();
args.insert(0, *receiver);
let receiver = &self.infer.type_of_expr[*receiver];
if receiver.strip_references().is_unknown() {
// if the receiver is of unknown type, it's very likely we
// don't know enough to correctly resolve the method call.
// This is kind of a band-aid for #6975.
return None;
}
// FIXME: note that we erase information about substs here. This
// is not right, but, luckily, doesn't matter as we care only
// about the number of params
@ -504,6 +512,22 @@ fn f() {
);
}
#[test]
fn method_unknown_receiver() {
// note: this is incorrect code, so there might be errors on this in the
// future, but we shouldn't emit an argument count diagnostic here
check_diagnostics(
r#"
trait Foo { fn method(&self, arg: usize) {} }
fn f() {
let x;
x.method();
}
"#,
);
}
#[test]
fn tuple_struct() {
check_diagnostics(

View File

@ -791,6 +791,10 @@ pub fn is_never(&self) -> bool {
matches!(self, Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }))
}
pub fn is_unknown(&self) -> bool {
matches!(self, Ty::Unknown)
}
/// If this is a `dyn Trait` type, this returns the `Trait` part.
pub fn dyn_trait_ref(&self) -> Option<&TraitRef> {
match self {