Skip to content

Commit b4a7cae

Browse files
Merge #7125
7125: Don't emit arg count diagnostics for method calls with unknown receiver r=flodiebold a=flodiebold Fixes #7098. Co-authored-by: Florian Diebold <[email protected]>
2 parents c92c9fd + 29acd39 commit b4a7cae

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

crates/hir_ty/src/diagnostics/expr.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
156156
// FIXME: Due to shortcomings in the current type system implementation, only emit this
157157
// diagnostic if there are no type mismatches in the containing function.
158158
if self.infer.type_mismatches.iter().next().is_some() {
159-
return Some(());
159+
return None;
160160
}
161161

162162
let is_method_call = matches!(expr, Expr::MethodCall { .. });
@@ -170,6 +170,14 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
170170
let mut args = args.clone();
171171
args.insert(0, *receiver);
172172

173+
let receiver = &self.infer.type_of_expr[*receiver];
174+
if receiver.strip_references().is_unknown() {
175+
// if the receiver is of unknown type, it's very likely we
176+
// don't know enough to correctly resolve the method call.
177+
// This is kind of a band-aid for #6975.
178+
return None;
179+
}
180+
173181
// FIXME: note that we erase information about substs here. This
174182
// is not right, but, luckily, doesn't matter as we care only
175183
// about the number of params
@@ -504,6 +512,22 @@ fn f() {
504512
);
505513
}
506514

515+
#[test]
516+
fn method_unknown_receiver() {
517+
// note: this is incorrect code, so there might be errors on this in the
518+
// future, but we shouldn't emit an argument count diagnostic here
519+
check_diagnostics(
520+
r#"
521+
trait Foo { fn method(&self, arg: usize) {} }
522+
523+
fn f() {
524+
let x;
525+
x.method();
526+
}
527+
"#,
528+
);
529+
}
530+
507531
#[test]
508532
fn tuple_struct() {
509533
check_diagnostics(

crates/hir_ty/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,10 @@ impl Ty {
791791
matches!(self, Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }))
792792
}
793793

794+
pub fn is_unknown(&self) -> bool {
795+
matches!(self, Ty::Unknown)
796+
}
797+
794798
/// If this is a `dyn Trait` type, this returns the `Trait` part.
795799
pub fn dyn_trait_ref(&self) -> Option<&TraitRef> {
796800
match self {

0 commit comments

Comments
 (0)