Skip to content

Commit

Permalink
Deny use<> for RPITITs
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jun 24, 2024
1 parent dd557d8 commit 6521c39
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 45 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_ast_lowering/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ ast_lowering_never_pattern_with_guard =
ast_lowering_no_precise_captures_on_apit = `use<...>` precise capturing syntax not allowed in argument-position `impl Trait`
ast_lowering_no_precise_captures_on_rpitit = `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
.note = currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope
ast_lowering_previously_used_here = previously used here
ast_lowering_register1 = register `{$reg1_name}`
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_ast_lowering/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,14 @@ pub(crate) struct NoPreciseCapturesOnApit {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_lowering_no_precise_captures_on_rpitit)]
#[note]
pub(crate) struct NoPreciseCapturesOnRpitit {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_lowering_yield_in_closure)]
pub(crate) struct YieldInClosure {
Expand Down
20 changes: 20 additions & 0 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
};
debug!(?captured_lifetimes_to_duplicate);

match fn_kind {
// Deny `use<>` on RPITIT in trait/trait-impl for now.
Some(FnDeclKind::Trait | FnDeclKind::Impl) => {
if let Some(span) = bounds.iter().find_map(|bound| match *bound {
ast::GenericBound::Use(_, span) => Some(span),
_ => None,
}) {
self.tcx.dcx().emit_err(errors::NoPreciseCapturesOnRpitit { span });
}
}
None
| Some(
FnDeclKind::Fn
| FnDeclKind::Inherent
| FnDeclKind::ExternFn
| FnDeclKind::Closure
| FnDeclKind::Pointer,
) => {}
}

self.lower_opaque_inner(
opaque_ty_node_id,
origin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fn type_param<T>() -> impl Sized + use<> {}
trait Foo {
fn bar() -> impl Sized + use<>;
//~^ ERROR `impl Trait` must mention the `Self` type of the trait
//~| ERROR `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
error: `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
--> $DIR/forgot-to-capture-type.rs:7:30
|
LL | fn bar() -> impl Sized + use<>;
| ^^^^^
|
= note: currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope

error: `impl Trait` must mention all type parameters in scope in `use<...>`
--> $DIR/forgot-to-capture-type.rs:3:23
|
Expand All @@ -18,5 +26,5 @@ LL | fn bar() -> impl Sized + use<>;
|
= note: currently, all type parameters are required to be mentioned in the precise captures list

error: aborting due to 2 previous errors
error: aborting due to 3 previous errors

20 changes: 20 additions & 0 deletions tests/ui/impl-trait/precise-capturing/redundant.normal.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
warning: all possible in-scope parameters are already captured, so `use<...>` syntax is redundant
--> $DIR/redundant.rs:7:19
|
LL | fn hello<'a>() -> impl Sized + use<'a> {}
| ^^^^^^^^^^^^^-------
| |
| help: remove the `use<...>` syntax
|
= note: `#[warn(impl_trait_redundant_captures)]` on by default

warning: all possible in-scope parameters are already captured, so `use<...>` syntax is redundant
--> $DIR/redundant.rs:12:27
|
LL | fn inherent(&self) -> impl Sized + use<'_> {}
| ^^^^^^^^^^^^^-------
| |
| help: remove the `use<...>` syntax

warning: 2 warnings emitted

18 changes: 18 additions & 0 deletions tests/ui/impl-trait/precise-capturing/redundant.rpitit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
--> $DIR/redundant.rs:18:35
|
LL | fn in_trait() -> impl Sized + use<'a, Self>;
| ^^^^^^^^^^^^^
|
= note: currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope

error: `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
--> $DIR/redundant.rs:23:35
|
LL | fn in_trait() -> impl Sized + use<'a> {}
| ^^^^^^^
|
= note: currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope

error: aborting due to 2 previous errors

13 changes: 8 additions & 5 deletions tests/ui/impl-trait/precise-capturing/redundant.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
//@ compile-flags: -Zunstable-options --edition=2024
//@ check-pass
//@ revisions: normal rpitit
//@[normal] check-pass

#![feature(precise_capturing)]

fn hello<'a>() -> impl Sized + use<'a> {}
//~^ WARN all possible in-scope parameters are already captured
//[normal]~^ WARN all possible in-scope parameters are already captured

struct Inherent;
impl Inherent {
fn inherent(&self) -> impl Sized + use<'_> {}
//~^ WARN all possible in-scope parameters are already captured
//[normal]~^ WARN all possible in-scope parameters are already captured
}

#[cfg(rpitit)]
trait Test<'a> {
fn in_trait() -> impl Sized + use<'a, Self>;
//~^ WARN all possible in-scope parameters are already captured
//[rpitit]~^ ERROR `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
}
#[cfg(rpitit)]
impl<'a> Test<'a> for () {
fn in_trait() -> impl Sized + use<'a> {}
//~^ WARN all possible in-scope parameters are already captured
//[rpitit]~^ ERROR `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
}

fn main() {}
36 changes: 0 additions & 36 deletions tests/ui/impl-trait/precise-capturing/redundant.stderr

This file was deleted.

10 changes: 9 additions & 1 deletion tests/ui/impl-trait/precise-capturing/rpitit.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
error: `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
--> $DIR/rpitit.rs:11:36
|
LL | fn hello() -> impl PartialEq + use<Self>;
| ^^^^^^^^^
|
= note: currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope

error: `impl Trait` captures lifetime parameter, but it is not mentioned in `use<...>` precise captures list
--> $DIR/rpitit.rs:11:19
|
Expand Down Expand Up @@ -38,5 +46,5 @@ LL | | );

help: `'a` and `'b` must be the same: replace one with the other

error: aborting due to 3 previous errors
error: aborting due to 4 previous errors

3 changes: 1 addition & 2 deletions tests/ui/impl-trait/precise-capturing/self-capture.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//@ check-pass

#![feature(precise_capturing)]

trait Foo {
fn bar<'a>() -> impl Sized + use<Self>;
//~^ ERROR `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
}

fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/impl-trait/precise-capturing/self-capture.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
--> $DIR/self-capture.rs:4:34
|
LL | fn bar<'a>() -> impl Sized + use<Self>;
| ^^^^^^^^^
|
= note: currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope

error: aborting due to 1 previous error

0 comments on commit 6521c39

Please sign in to comment.