Skip to content

Commit

Permalink
Rollup merge of rust-lang#96903 - oli-obk:opaque_type_lifetime_constr…
Browse files Browse the repository at this point in the history
…aints, r=compiler-errors

Use lifetimes on type-alias-impl-trait used in function signatures to infer output type lifetimes

fixes rust-lang#96564

TLDR:

```rust
fn execute(ty: Ty<'_>) -> &str { todo!() }
```

(`Ty` being a type alias impl trait) used to produce the following error before this PR

```
error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types
 --> src/lib.rs:4:27
  |
4 | fn execute(ty: Ty<'_>) -> &str { todo!() }
  |                           ^^^^
  |
  = note: lifetimes appearing in an associated type are not considered constrained
```
  • Loading branch information
matthiaskrgr authored May 10, 2022
2 parents a402a00 + d63f82e commit f80348b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@ impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
// ignore the inputs to a projection, as they may not appear
// in the normalized form
if self.just_constrained {
if let ty::Projection(..) | ty::Opaque(..) = t.kind() {
if let ty::Projection(..) = t.kind() {
return ControlFlow::CONTINUE;
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/type-alias-impl-trait/constrain_inputs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// check-pass

#![feature(type_alias_impl_trait)]

mod foo {
type Ty<'a> = impl Sized;
fn defining(s: &str) -> Ty<'_> { s }
fn execute(ty: Ty<'_>) -> &str { todo!() }
}

mod bar {
type Ty<'a> = impl FnOnce() -> &'a str;
fn defining(s: &str) -> Ty<'_> { move || s }
fn execute(ty: Ty<'_>) -> &str { ty() }
}

fn main() {}

0 comments on commit f80348b

Please sign in to comment.