-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Emits E0599 when meeting MyTrait::missing_method
#111588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1245,6 +1245,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
error, | ||
Some((rcvr, args)), | ||
expected, | ||
false, | ||
) { | ||
err.emit(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ use crate::rvalue_scopes; | |
use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LocalTy, RawTy}; | ||
use rustc_data_structures::captures::Captures; | ||
use rustc_data_structures::fx::FxHashSet; | ||
use rustc_errors::{Applicability, Diagnostic, ErrorGuaranteed, MultiSpan}; | ||
use rustc_errors::{Applicability, Diagnostic, ErrorGuaranteed, MultiSpan, StashKey}; | ||
use rustc_hir as hir; | ||
use rustc_hir::def::{CtorOf, DefKind, Res}; | ||
use rustc_hir::def_id::DefId; | ||
|
@@ -853,6 +853,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
let item_name = item_segment.ident; | ||
let result = self | ||
.resolve_fully_qualified_call(span, item_name, ty.normalized, qself.span, hir_id) | ||
.and_then(|r| { | ||
// lint bare trait if the method is found in the trait | ||
if span.edition().rust_2021() && let Some(mut diag) = self.tcx.sess.diagnostic().steal_diagnostic(qself.span, StashKey::TraitMissingMethod) { | ||
diag.emit(); | ||
} | ||
Ok(r) | ||
}) | ||
.or_else(|error| { | ||
let guar = self | ||
.tcx | ||
|
@@ -863,17 +870,30 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
_ => Err(guar), | ||
}; | ||
|
||
let trait_missing_method = | ||
matches!(error, method::MethodError::NoMatch(_)) && ty.normalized.is_trait(); | ||
// If we have a path like `MyTrait::missing_method`, then don't register | ||
// a WF obligation for `dyn MyTrait` when method lookup fails. Otherwise, | ||
// register a WF obligation so that we can detect any additional | ||
// errors in the self type. | ||
if !(matches!(error, method::MethodError::NoMatch(_)) && ty.normalized.is_trait()) { | ||
if !trait_missing_method { | ||
self.register_wf_obligation( | ||
ty.raw.into(), | ||
qself.span, | ||
traits::WellFormed(None), | ||
); | ||
} | ||
|
||
// emit or cancel the diagnostic for bare traits | ||
if span.edition().rust_2021() && let Some(mut diag) = self.tcx.sess.diagnostic().steal_diagnostic(qself.span, StashKey::TraitMissingMethod) { | ||
if trait_missing_method { | ||
// cancel the diag for bare traits when meeting `MyTrait::missing_method` | ||
diag.cancel(); | ||
} else { | ||
diag.emit(); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this code be in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is good to pass only |
||
if item_name.name != kw::Empty { | ||
if let Some(mut e) = self.report_method_error( | ||
span, | ||
|
@@ -883,10 +903,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
error, | ||
None, | ||
Expectation::NoExpectation, | ||
trait_missing_method && span.edition().rust_2021(), // emits missing method for trait only after edition 2021 | ||
) { | ||
e.emit(); | ||
} | ||
} | ||
|
||
result | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// edition: 2021 | ||
|
||
trait Has { | ||
fn has() {} | ||
} | ||
|
||
trait HasNot {} | ||
|
||
fn main() { | ||
HasNot::has(); //~ ERROR | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0599]: no function or associated item named `has` found for trait `HasNot` | ||
--> $DIR/issue-111312.rs:10:13 | ||
| | ||
LL | HasNot::has(); | ||
| ^^^ function or associated item not found in `HasNot` | ||
| | ||
note: `Has` defines an item `has` | ||
--> $DIR/issue-111312.rs:3:1 | ||
| | ||
LL | trait Has { | ||
| ^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0599`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#[derive(Clone)] //~ trait objects must include the `dyn` keyword | ||
//~| trait objects must include the `dyn` keyword | ||
struct Foo; | ||
trait Foo {} //~ the name `Foo` is defined multiple times | ||
fn main() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this redundant with the code below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. This code emits steal and emit the diag when it is able to find the target method for
dyn MyTrait
. The code below is executed only when it is not able to find such a method.