Skip to content

Commit

Permalink
Rollup merge of rust-lang#114147 - compiler-errors:missing-rpitits, r…
Browse files Browse the repository at this point in the history
…=spastorino

Insert RPITITs that were shadowed by missing ADTs that resolve to [type error]

Comment inline explains how this can happen.

Fixes rust-lang#113903
  • Loading branch information
matthiaskrgr committed Jul 28, 2023
2 parents 06eebbe + a4ac773 commit 76f0a8c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
26 changes: 22 additions & 4 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
);
ocx.resolve_regions_and_report_errors(impl_m_def_id, &outlives_env)?;

let mut collected_tys = FxHashMap::default();
let mut remapped_types = FxHashMap::default();
for (def_id, (ty, args)) in collected_types {
match infcx.fully_resolve((ty, args)) {
Ok((ty, args)) => {
Expand Down Expand Up @@ -806,19 +806,37 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
Ok(ty) => ty,
Err(guar) => Ty::new_error(tcx, guar),
};
collected_tys.insert(def_id, ty::EarlyBinder::bind(ty));
remapped_types.insert(def_id, ty::EarlyBinder::bind(ty));
}
Err(err) => {
let reported = tcx.sess.delay_span_bug(
return_span,
format!("could not fully resolve: {ty} => {err:?}"),
);
collected_tys.insert(def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, reported)));
remapped_types.insert(def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, reported)));
}
}
}

Ok(&*tcx.arena.alloc(collected_tys))
// We may not collect all RPITITs that we see in the HIR for a trait signature
// because an RPITIT was located within a missing item. Like if we have a sig
// returning `-> Missing<impl Sized>`, that gets converted to `-> [type error]`,
// and when walking through the signature we end up never collecting the def id
// of the `impl Sized`. Insert that here, so we don't ICE later.
for assoc_item in tcx.associated_types_for_impl_traits_in_associated_fn(trait_m.def_id) {
if !remapped_types.contains_key(assoc_item) {
remapped_types.insert(
*assoc_item,
ty::EarlyBinder::bind(Ty::new_error_with_message(
tcx,
return_span,
"missing synthetic item for RPITIT",
)),
);
}
}

Ok(&*tcx.arena.alloc(remapped_types))
}

struct ImplTraitInTraitCollector<'a, 'tcx> {
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/impl-trait/in-trait/rpitit-shadowed-by-missing-adt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// issue: 113903

#![feature(return_position_impl_trait_in_trait)]

use std::ops::Deref;

pub trait Tr {
fn w() -> impl Deref<Target = Missing<impl Sized>>;
//~^ ERROR cannot find type `Missing` in this scope
}

impl Tr for () {
fn w() -> &'static () {
&()
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0412]: cannot find type `Missing` in this scope
--> $DIR/rpitit-shadowed-by-missing-adt.rs:8:35
|
LL | fn w() -> impl Deref<Target = Missing<impl Sized>>;
| ^^^^^^^ not found in this scope

error: aborting due to previous error

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

0 comments on commit 76f0a8c

Please sign in to comment.