Skip to content

Commit

Permalink
Auto merge of #101682 - compiler-errors:rpitit-encode, r=fee1-dead
Browse files Browse the repository at this point in the history
Only encode return-position `impl Trait` in trait when parent function has a default body

Semi-blocked on #101679, because I can't currently write a test for when we _should_ encode the type of the return-position `impl Trait` in trait, which is when a trait has a default function body, like so:

```rust
trait Foo {
  fn bar() -> impl Sized { }
}
```

Though this can land even without #101679, since it does prevent ICEs from occuring any time you use `#![feature(return_position_impl_trait_in_trait)]` in a library, which is kind annoying.
  • Loading branch information
bors committed Sep 11, 2022
2 parents 6f0c4a6 + 1335da9 commit 17a627f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
14 changes: 13 additions & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,6 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
| DefKind::Static(..)
| DefKind::TyAlias
| DefKind::OpaqueTy
| DefKind::ImplTraitPlaceholder
| DefKind::ForeignTy
| DefKind::Impl
| DefKind::AssocFn
Expand All @@ -1047,6 +1046,19 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
| DefKind::AnonConst
| DefKind::InlineConst => true,

DefKind::ImplTraitPlaceholder => {
let parent_def_id = tcx.impl_trait_in_trait_parent(def_id.to_def_id());
let assoc_item = tcx.associated_item(parent_def_id);
match assoc_item.container {
// Always encode an RPIT in an impl fn, since it always has a body
ty::AssocItemContainer::ImplContainer => true,
ty::AssocItemContainer::TraitContainer => {
// Encode an RPIT for a trait only if the trait has a default body
assoc_item.defaultness(tcx).has_value()
}
}
}

DefKind::AssocTy => {
let assoc_item = tcx.associated_item(def_id);
match assoc_item.container {
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/impl-trait/in-trait/encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// build-pass
// compile-flags: --crate-type=lib

#![feature(return_position_impl_trait_in_trait)]
#![allow(incomplete_features)]

trait Foo {
fn bar() -> impl Sized;
}

0 comments on commit 17a627f

Please sign in to comment.