Skip to content

On argument position impl Trait<A> where A is not found, hide useles error #132082

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2210,6 +2210,41 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
bounds.iter().map(move |bound| self.lower_param_bound(bound, itctx))
}

fn lower_res_for_universal_param_and_bounds(
&mut self,
def_id: LocalDefId,
pred: &Option<hir::WherePredicate<'hir>>,
) -> Res {
if let Some(predicate) = pred
&& let hir::WherePredicate::BoundPredicate(bound_pred) = predicate
&& bound_pred.bounds.iter().any(|bound| {
let mut references_error = false;
if let hir::GenericBound::Trait(poly) = bound {
for segment in poly.trait_ref.path.segments {
for arg in segment.args().args {
if let hir::GenericArg::Type(ty) = arg
&& let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = ty.kind
&& let Res::Err = path.res
{
references_error = true;
break;
}
}
}
}
references_error
})
{
// When any of the arguments of an `impl Trait<A, B, C>` couldn't be resolved, we mark
// the type parameter corresponding to the `impl Trait` as `Err` in order to avoid
// knock down errors, particularly in trait selection, as if `A` isn't resolved,
// `impl Trait<A>: Trait<A>` *doesn't* hold.
Res::Err
} else {
Res::Def(DefKind::TyParam, def_id.to_def_id())
}
}

#[instrument(level = "debug", skip(self), ret)]
fn lower_universal_param_and_bounds(
&mut self,
Expand Down Expand Up @@ -2246,7 +2281,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
);

let hir_id = self.next_id();
let res = Res::Def(DefKind::TyParam, def_id.to_def_id());
let res = self.lower_res_for_universal_param_and_bounds(def_id, &preds);
let ty = hir::TyKind::Path(hir::QPath::Resolved(
None,
self.arena.alloc(hir::Path {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Ensure that we don't emit an E0270 for "`impl AsRef<Path>: AsRef<Path>` not satisfied".

fn foo(filename: impl AsRef<Path>) { //~ ERROR cannot find type `Path` in this scope
std::fs::write(filename, "hello").unwrap();
}

fn main() {
foo("/tmp/hello");
}
14 changes: 14 additions & 0 deletions tests/ui/impl-trait/resolve-error-in-impl-trait-fn-argument.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0412]: cannot find type `Path` in this scope
--> $DIR/resolve-error-in-impl-trait-fn-argument.rs:3:29
|
LL | fn foo(filename: impl AsRef<Path>) {
| ^^^^ not found in this scope
|
help: consider importing this struct
|
LL + use std::path::Path;
|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0412`.
Loading