Change:
<!-- Thank you for filing a bug report! Please provide a short summary of the bug, along with any information you feel relevant to replicating the bug. If you cannot produce a minimal reproduction case (something that would work in isolation), please provide the steps or even link to a repository that causes the problematic output to occur. --Given the following code: <!-- Please provide a link to --The current output is: <!-- The following is not always necessary. --Ideally the output should suggest -ing the out-of-scope trait, similar to E0599: <!-- If the problem is not self-explanatory, please provide a rationale for the change. --Someone tried to do dynamic dispatch between two underlying used a boxed trait object (with the type spelled , so wasn't in scope) and call the method on the boxed reader, and it was unclear from the error why the method wasn't accessible: <!-- If dramatically different output is caused by small changes, consider also adding them here. If you're using the stable version of the compiler, you should also check if the bug also exists in the beta or nightly versions. The output might also be different depending on the Edition. --This is reproducible on nightly (rustc 1.67.0-nightly ( 2022-12-01)).
Note: A sign there may be a problem worth fixing here is the code I wrote is a near copypaste of a top stack overflow answer for a :)
So, after some investigating, this appears to be a regression of , which essentially the same suggestion I proposed here: adding a for the trait object's trait. When generating the error when the lookup fails, the trait is initially included in the list of candidates, but it's filtered out by this (because the callee is the trait method, and so the parent of the callee is the trait, which is the candidate): This was introduced in by which also removed the corresponding suggestion from the test's expected output. It's not immediately clear to me from the PR () why this change was necessary. If the call is removed, the only UI test that fails is the one. Reverting that change would fix this issue, but I'm not sure if there are other cases I'm missing. (cc with whom I was discussing this on Mastodon)
Haven't look at this yet, maybe I misinterpreted what was achieving, and that commit just needs a revert -- can't recall why I did that.
Yeah, lol, I totally overlooked that the would be load-bearing due to the way we treat trait methods on as inherent. I'll revert , modify the test, etc.

Discussion:
use rustc_infer::infer::{self, InferOk}; use rustc_middle::traits::ObligationCause; use rustc_middle::ty::subst::{InternalSubsts, SubstsRef}; <del> use rustc_middle::ty::{self, DefIdTree, GenericParamDefKind, Ty, TypeVisitable}; </del> <ins> use rustc_middle::ty::{self, GenericParamDefKind, Ty, TypeVisitable}; </ins> use rustc_span::symbol::Ident; use rustc_span::Span; use rustc_trait_selection::traits;

} // We probe again, taking all traits into account (not only those in scope). <del> let mut candidates = </del> <ins> let candidates = </ins> match self.lookup_probe(segment.ident, self_ty, call_expr, ProbeScope::AllTraits) { // If we find a different result the caller probably forgot to import a trait. Ok(ref new_pick) if pick.differs_from(new_pick) => {

.collect(), _ => Vec::new(), }; <del> candidates.retain(|candidate| *candidate != self.tcx.parent(result.callee.def_id)); </del> return Err(IllegalSizedBound(candidates, needs_mut, span)); }

<ins> // revisions: imported unimported //[imported] check-pass </ins> mod private { pub trait Future { <ins> //[unimported]~^^ HELP perhaps add a `use` for it </ins> fn wait(&self) where Self: Sized; }

} } <del> //use private::Future; </del> <ins> #[cfg(imported)] use private::Future; </ins> fn bar(arg: Box<dyn private::Future>) { <ins> // Importing the trait means that we don't autoderef `Box<dyn Future>` </ins> arg.wait(); <del> //~^ ERROR the `wait` method cannot be invoked on a trait object </del> <ins> //[unimported]~^ ERROR the `wait` method cannot be invoked on a trait object </ins> } <del> fn main() { } </del> <ins> fn main() {} </ins>

<del> error: the `wait` method cannot be invoked on a trait object --> $DIR/issue-35976.rs:14:9 | LL |         fn wait(&self) where Self: Sized; |                                    ----- this has a `Sized` requirement ... LL |     arg.wait(); |         ^^^^ error: aborting due to previous error </del>

<ins> error: the `wait` method cannot be invoked on a trait object --> $DIR/issue-35976.rs:20:9 | LL |         fn wait(&self) where Self: Sized; |                                    ----- this has a `Sized` requirement ... LL |     arg.wait(); |         ^^^^ | help: another candidate was found in the following trait, perhaps add a `use` for it: | LL | use private::Future; | error: aborting due to previous error </ins>