Skip to content

Commit 4c83bd0

Browse files
committed
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
2 parents 19423b5 + 7e0b1f1 commit 4c83bd0

File tree

10 files changed

+61
-3
lines changed

10 files changed

+61
-3
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2602,7 +2602,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26022602
match path.res {
26032603
Res::Def(DefKind::OpaqueTy | DefKind::ImplTraitPlaceholder, did) => {
26042604
// Check for desugared `impl Trait`.
2605-
assert!(ty::is_impl_trait_defn(tcx, did).is_none());
2605+
assert!(tcx.is_type_alias_impl_trait(did));
26062606
let item_segment = path.segments.split_last().unwrap();
26072607
self.prohibit_generics(item_segment.1.iter(), |err| {
26082608
err.note("`impl Trait` types can't have type parameters");

compiler/rustc_hir_analysis/src/collect.rs

+11
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub fn provide(providers: &mut Providers) {
7676
is_foreign_item,
7777
generator_kind,
7878
collect_mod_item_types,
79+
is_type_alias_impl_trait,
7980
..*providers
8081
};
8182
}
@@ -1537,3 +1538,13 @@ fn generator_kind(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::GeneratorKind>
15371538
_ => bug!("generator_kind applied to non-local def-id {:?}", def_id),
15381539
}
15391540
}
1541+
1542+
fn is_type_alias_impl_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
1543+
match tcx.hir().get_if_local(def_id) {
1544+
Some(Node::Item(hir::Item { kind: hir::ItemKind::OpaqueTy(opaque), .. })) => {
1545+
matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias)
1546+
}
1547+
Some(_) => bug!("tried getting opaque_ty_origin for non-opaque: {:?}", def_id),
1548+
_ => bug!("tried getting opaque_ty_origin for non-local def-id {:?}", def_id),
1549+
}
1550+
}

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+9
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ provide! { tcx, def_id, other, cdata,
223223
generator_kind => { table }
224224
trait_def => { table }
225225
deduced_param_attrs => { table }
226+
is_type_alias_impl_trait => {
227+
debug_assert_eq!(tcx.def_kind(def_id), DefKind::OpaqueTy);
228+
cdata
229+
.root
230+
.tables
231+
.is_type_alias_impl_trait
232+
.get(cdata, def_id.index)
233+
.is_some()
234+
}
226235
collect_return_position_impl_trait_in_trait_tys => {
227236
Ok(cdata
228237
.root

compiler/rustc_metadata/src/rmeta/encoder.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1512,8 +1512,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15121512
hir::ItemKind::Mod(ref m) => {
15131513
return self.encode_info_for_mod(item.owner_id.def_id, m);
15141514
}
1515-
hir::ItemKind::OpaqueTy(..) => {
1515+
hir::ItemKind::OpaqueTy(ref opaque) => {
15161516
self.encode_explicit_item_bounds(def_id);
1517+
if matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias) {
1518+
self.tables.is_type_alias_impl_trait.set(def_id.index, ());
1519+
}
15171520
}
15181521
hir::ItemKind::Enum(..) => {
15191522
let adt_def = self.tcx.adt_def(def_id);

compiler/rustc_metadata/src/rmeta/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ define_tables! {
404404
proc_macro: Table<DefIndex, MacroKind>,
405405
module_reexports: Table<DefIndex, LazyArray<ModChild>>,
406406
deduced_param_attrs: Table<DefIndex, LazyArray<DeducedParamAttrs>>,
407+
// Slot is full when opaque is TAIT.
408+
is_type_alias_impl_trait: Table<DefIndex, ()>,
407409

408410
trait_impl_trait_tys: Table<DefIndex, LazyValue<FxHashMap<DefId, Ty<'static>>>>,
409411
}

compiler/rustc_middle/src/query/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ rustc_queries! {
177177
separate_provide_extern
178178
}
179179

180+
query is_type_alias_impl_trait(key: DefId) -> bool
181+
{
182+
desc { "determine whether the opaque is a type-alias impl trait" }
183+
separate_provide_extern
184+
}
185+
180186
query analysis(key: ()) -> Result<(), ErrorGuaranteed> {
181187
eval_always
182188
desc { "running analysis passes on this crate" }

compiler/rustc_middle/src/ty/parameterized.rs

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ trivially_parameterized_over_tcx! {
5252
usize,
5353
(),
5454
u32,
55+
bool,
5556
std::string::String,
5657
crate::metadata::ModChild,
5758
crate::middle::codegen_fn_attrs::CodegenFnAttrs,

compiler/rustc_trait_selection/src/traits/wf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ impl<'tcx> WfPredicates<'tcx> {
654654
// All of the requirements on type parameters
655655
// have already been checked for `impl Trait` in
656656
// return position. We do need to check type-alias-impl-trait though.
657-
if ty::is_impl_trait_defn(self.tcx, def_id).is_none() {
657+
if self.tcx.is_type_alias_impl_trait(def_id) {
658658
let obligations = self.nominal_obligations(def_id, substs);
659659
self.out.extend(obligations);
660660
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// edition:2021
2+
3+
pub trait T {}
4+
impl T for () {}
5+
6+
pub struct S {}
7+
8+
impl S {
9+
pub async fn f<'a>(&self) -> impl T + 'a {
10+
()
11+
}
12+
}

tests/ui/async-await/issue-107036.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// aux-build:issue-107036.rs
2+
// edition:2021
3+
// check-pass
4+
5+
extern crate issue_107036;
6+
use issue_107036::S;
7+
8+
async fn f() {
9+
S{}.f().await;
10+
}
11+
12+
fn main() {
13+
let _ = f();
14+
}

0 commit comments

Comments
 (0)