Auto merge of #107038 - compiler-errors:dont-wfcheck-non-local-rpit, r=oli-obk

Don't wf-check non-local RPITs

We were using `ty::is_impl_trait_defn(..).is_none()` to check if we need to add WF obligations for an opaque type.

This is *supposed* to be checking if the type is a TAIT, since RPITs' wfness is implied by wf checking its parent item, but since `is_impl_trait_defn` returns `None` for non-local RPIT and async futures, we unnecessarily consider wf predicates for an RPIT if it is coming from a foreign crate.

Fixes #107036

r? `@oli-obk` but feel free to reassign
This commit is contained in:
bors 2023-01-19 16:49:06 +00:00
commit 4c83bd03a9
10 changed files with 61 additions and 3 deletions

View File

@ -2602,7 +2602,7 @@ pub fn res_to_ty(
match path.res {
Res::Def(DefKind::OpaqueTy | DefKind::ImplTraitPlaceholder, did) => {
// Check for desugared `impl Trait`.
assert!(ty::is_impl_trait_defn(tcx, did).is_none());
assert!(tcx.is_type_alias_impl_trait(did));
let item_segment = path.segments.split_last().unwrap();
self.prohibit_generics(item_segment.1.iter(), |err| {
err.note("`impl Trait` types can't have type parameters");

View File

@ -76,6 +76,7 @@ pub fn provide(providers: &mut Providers) {
is_foreign_item,
generator_kind,
collect_mod_item_types,
is_type_alias_impl_trait,
..*providers
};
}
@ -1537,3 +1538,13 @@ fn generator_kind(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::GeneratorKind>
_ => bug!("generator_kind applied to non-local def-id {:?}", def_id),
}
}
fn is_type_alias_impl_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
match tcx.hir().get_if_local(def_id) {
Some(Node::Item(hir::Item { kind: hir::ItemKind::OpaqueTy(opaque), .. })) => {
matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias)
}
Some(_) => bug!("tried getting opaque_ty_origin for non-opaque: {:?}", def_id),
_ => bug!("tried getting opaque_ty_origin for non-local def-id {:?}", def_id),
}
}

View File

@ -223,6 +223,15 @@ fn into_args(self) -> (DefId, SimplifiedType) {
generator_kind => { table }
trait_def => { table }
deduced_param_attrs => { table }
is_type_alias_impl_trait => {
debug_assert_eq!(tcx.def_kind(def_id), DefKind::OpaqueTy);
cdata
.root
.tables
.is_type_alias_impl_trait
.get(cdata, def_id.index)
.is_some()
}
collect_return_position_impl_trait_in_trait_tys => {
Ok(cdata
.root

View File

@ -1512,8 +1512,11 @@ fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'tcx>) {
hir::ItemKind::Mod(ref m) => {
return self.encode_info_for_mod(item.owner_id.def_id, m);
}
hir::ItemKind::OpaqueTy(..) => {
hir::ItemKind::OpaqueTy(ref opaque) => {
self.encode_explicit_item_bounds(def_id);
if matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias) {
self.tables.is_type_alias_impl_trait.set(def_id.index, ());
}
}
hir::ItemKind::Enum(..) => {
let adt_def = self.tcx.adt_def(def_id);

View File

@ -404,6 +404,8 @@ fn encode(&self, buf: &mut FileEncoder) -> LazyTables {
proc_macro: Table<DefIndex, MacroKind>,
module_reexports: Table<DefIndex, LazyArray<ModChild>>,
deduced_param_attrs: Table<DefIndex, LazyArray<DeducedParamAttrs>>,
// Slot is full when opaque is TAIT.
is_type_alias_impl_trait: Table<DefIndex, ()>,
trait_impl_trait_tys: Table<DefIndex, LazyValue<FxHashMap<DefId, Ty<'static>>>>,
}

View File

@ -177,6 +177,12 @@
separate_provide_extern
}
query is_type_alias_impl_trait(key: DefId) -> bool
{
desc { "determine whether the opaque is a type-alias impl trait" }
separate_provide_extern
}
query analysis(key: ()) -> Result<(), ErrorGuaranteed> {
eval_always
desc { "running analysis passes on this crate" }

View File

@ -52,6 +52,7 @@ impl $crate::ty::ParameterizedOverTcx for $ty {
usize,
(),
u32,
bool,
std::string::String,
crate::metadata::ModChild,
crate::middle::codegen_fn_attrs::CodegenFnAttrs,

View File

@ -654,7 +654,7 @@ fn compute(&mut self, arg: GenericArg<'tcx>) {
// All of the requirements on type parameters
// have already been checked for `impl Trait` in
// return position. We do need to check type-alias-impl-trait though.
if ty::is_impl_trait_defn(self.tcx, def_id).is_none() {
if self.tcx.is_type_alias_impl_trait(def_id) {
let obligations = self.nominal_obligations(def_id, substs);
self.out.extend(obligations);
}

View File

@ -0,0 +1,12 @@
// edition:2021
pub trait T {}
impl T for () {}
pub struct S {}
impl S {
pub async fn f<'a>(&self) -> impl T + 'a {
()
}
}

View File

@ -0,0 +1,14 @@
// aux-build:issue-107036.rs
// edition:2021
// check-pass
extern crate issue_107036;
use issue_107036::S;
async fn f() {
S{}.f().await;
}
fn main() {
let _ = f();
}