Add hir::Node::PreciseCapturingNonLifetimeArg

This commit is contained in:
Michael Goulet 2024-04-04 14:46:26 -04:00
parent 42ba57c013
commit 02d7317af2
14 changed files with 110 additions and 38 deletions

View File

@ -385,4 +385,21 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
fn visit_pattern_type_pattern(&mut self, p: &'hir hir::Pat<'hir>) {
self.visit_pat(p)
}
fn visit_precise_capturing_arg(
&mut self,
arg: &'hir PreciseCapturingArg<'hir>,
) -> Self::Result {
match arg {
PreciseCapturingArg::Lifetime(_) => {
// This is represented as a `Node::Lifetime`, intravisit will get to it below.
}
PreciseCapturingArg::Param(param) => self.insert(
param.ident.span,
param.hir_id,
Node::PreciseCapturingNonLifetimeArg(param),
),
}
intravisit::walk_precise_capturing_arg(self, arg);
}
}

View File

@ -1790,11 +1790,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
PreciseCapturingArg::Lifetime(lt) => {
hir::PreciseCapturingArg::Lifetime(self.lower_lifetime(lt))
}
PreciseCapturingArg::Arg(_, node_id) => {
PreciseCapturingArg::Arg(ident, node_id) => {
let res = self.resolver.get_partial_res(*node_id).map_or(Res::Err, |partial_res| {
partial_res.full_res().expect("no partial res expected for precise capture arg")
});
hir::PreciseCapturingArg::Param(self.lower_res(res), self.lower_node_id(*node_id))
hir::PreciseCapturingArg::Param(hir::PreciseCapturingNonLifetimeArg {
hir_id: self.lower_node_id(*node_id),
ident: self.lower_ident(*ident),
res: self.lower_res(res),
})
}
}))
}

View File

@ -2565,7 +2565,14 @@ pub struct OpaqueTy<'hir> {
pub enum PreciseCapturingArg<'hir> {
Lifetime(&'hir Lifetime),
/// Non-lifetime argument (type or const)
Param(Res, HirId),
Param(PreciseCapturingNonLifetimeArg),
}
#[derive(Debug, Clone, Copy, HashStable_Generic)]
pub struct PreciseCapturingNonLifetimeArg {
pub hir_id: HirId,
pub ident: Ident,
pub res: Res,
}
/// From whence the opaque type came.
@ -3544,6 +3551,7 @@ pub enum Node<'hir> {
WhereBoundPredicate(&'hir WhereBoundPredicate<'hir>),
// FIXME: Merge into `Node::Infer`.
ArrayLenInfer(&'hir InferArg),
PreciseCapturingNonLifetimeArg(&'hir PreciseCapturingNonLifetimeArg),
// Created by query feeding
Synthetic,
// Span by reference to minimize `Node`'s size
@ -3580,6 +3588,7 @@ impl<'hir> Node<'hir> {
Node::TypeBinding(b) => Some(b.ident),
Node::PatField(f) => Some(f.ident),
Node::ExprField(f) => Some(f.ident),
Node::PreciseCapturingNonLifetimeArg(a) => Some(a.ident),
Node::Param(..)
| Node::AnonConst(..)
| Node::ConstBlock(..)

View File

@ -1151,7 +1151,7 @@ pub fn walk_precise_capturing_arg<'v, V: Visitor<'v>>(
) -> V::Result {
match *arg {
PreciseCapturingArg::Lifetime(lt) => visitor.visit_lifetime(lt),
PreciseCapturingArg::Param(_, hir_id) => visitor.visit_id(hir_id),
PreciseCapturingArg::Param(param) => visitor.visit_id(param.hir_id),
}
}

View File

@ -487,7 +487,9 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe
for arg in precise_capturing_args {
match *arg {
hir::PreciseCapturingArg::Lifetime(&hir::Lifetime { hir_id, .. })
| hir::PreciseCapturingArg::Param(_, hir_id) => match tcx.named_bound_var(hir_id) {
| hir::PreciseCapturingArg::Param(hir::PreciseCapturingNonLifetimeArg {
hir_id, ..
}) => match tcx.named_bound_var(hir_id) {
Some(ResolvedArg::EarlyBound(def_id)) => {
expected_captures.insert(def_id);
}

View File

@ -577,10 +577,10 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
});
}
},
hir::PreciseCapturingArg::Param(res, hir_id) => match res {
hir::PreciseCapturingArg::Param(param) => match param.res {
Res::Def(DefKind::TyParam | DefKind::ConstParam, def_id)
| Res::SelfTyParam { trait_: def_id } => {
self.resolve_type_ref(def_id.expect_local(), hir_id);
self.resolve_type_ref(def_id.expect_local(), param.hir_id);
}
Res::Err => {}
_ => {

View File

@ -99,6 +99,7 @@ impl<'a> State<'a> {
Node::PatField(a) => self.print_patfield(a),
Node::Arm(a) => self.print_arm(a),
Node::Infer(_) => self.word("_"),
Node::PreciseCapturingNonLifetimeArg(param) => self.print_ident(param.ident),
Node::Block(a) => {
// Containing cbox, will be closed by print-block at `}`.
self.cbox(INDENT_UNIT);

View File

@ -909,6 +909,7 @@ impl<'hir> Map<'hir> {
Node::Crate(item) => item.spans.inner_span,
Node::WhereBoundPredicate(pred) => pred.span,
Node::ArrayLenInfer(inf) => inf.span,
Node::PreciseCapturingNonLifetimeArg(param) => param.ident.span,
Node::Synthetic => unreachable!(),
Node::Err(span) => *span,
}
@ -1176,6 +1177,7 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
Node::ArrayLenInfer(_) => node_str("array len infer"),
Node::Synthetic => unreachable!(),
Node::Err(_) => node_str("error"),
Node::PreciseCapturingNonLifetimeArg(_param) => node_str("parameter"),
}
}

View File

@ -1,7 +1,10 @@
#![feature(precise_capturing)]
//~^ WARN the feature `precise_capturing` is incomplete
fn type_param<T>() -> impl use<> Sized {}
//~^ ERROR `impl Trait` must mention all type parameters in scope
fn lifetime_in_bounds<'a>(x: &'a ()) -> impl use<> Into<&'a ()> { x }
//~^ ERROR `impl Trait` captures lifetime parameter, but it is not mentioned in `use<...>` precise captures list
fn lifetime_in_hidden<'a>(x: &'a ()) -> impl use<> Sized { x }
//~^ ERROR hidden type for `impl Sized` captures lifetime that does not appear in bounds
fn main() {}

View File

@ -7,13 +7,29 @@ LL | #![feature(precise_capturing)]
= note: see issue #123432 <https://github.com/rust-lang/rust/issues/123432> for more information
= note: `#[warn(incomplete_features)]` on by default
error: `impl Trait` must mention all type parameters in scope
--> $DIR/forgot-to-capture-lifetime.rs:4:15
error: `impl Trait` captures lifetime parameter, but it is not mentioned in `use<...>` precise captures list
--> $DIR/forgot-to-capture-lifetime.rs:4:58
|
LL | fn type_param<T>() -> impl use<> Sized {}
| ^ ---------------- type parameter is implicitly captured by this `impl Trait`
LL | fn lifetime_in_bounds<'a>(x: &'a ()) -> impl use<> Into<&'a ()> { x }
| -- -----------------^^----
| | |
| | lifetime captured due to being mentioned in the bounds of the `impl Trait`
| this lifetime parameter is captured
error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds
--> $DIR/forgot-to-capture-lifetime.rs:7:60
|
= note: currently, all type parameters are required to be mentioned in the precise captures list
LL | fn lifetime_in_hidden<'a>(x: &'a ()) -> impl use<> Sized { x }
| -- ---------------- ^
| | |
| | opaque type defined here
| hidden type `&'a ()` captures the lifetime `'a` as defined here
|
help: to declare that `impl Sized` captures `'a`, you can add an explicit `'a` lifetime bound
|
LL | fn lifetime_in_hidden<'a>(x: &'a ()) -> impl use<> Sized + 'a { x }
| ++++
error: aborting due to 1 previous error; 1 warning emitted
error: aborting due to 2 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0700`.

View File

@ -1,10 +1,12 @@
#![feature(precise_capturing)]
//~^ WARN the feature `precise_capturing` is incomplete
fn lifetime_in_bounds<'a>(x: &'a ()) -> impl use<> Into<&'a ()> { x }
//~^ ERROR `impl Trait` captures lifetime parameter, but it is not mentioned in `use<...>` precise captures list
fn type_param<T>() -> impl use<> Sized {}
//~^ ERROR `impl Trait` must mention all type parameters in scope
fn lifetime_in_hidden<'a>(x: &'a ()) -> impl use<> Sized { x }
//~^ ERROR hidden type for `impl Sized` captures lifetime that does not appear in bounds
trait Foo {
//~^ ERROR `impl Trait` must mention all type parameters in scope
fn bar() -> impl use<> Sized;
}
fn main() {}

View File

@ -7,29 +7,24 @@ LL | #![feature(precise_capturing)]
= note: see issue #123432 <https://github.com/rust-lang/rust/issues/123432> for more information
= note: `#[warn(incomplete_features)]` on by default
error: `impl Trait` captures lifetime parameter, but it is not mentioned in `use<...>` precise captures list
--> $DIR/forgot-to-capture-type.rs:4:58
error: `impl Trait` must mention all type parameters in scope
--> $DIR/forgot-to-capture-type.rs:4:15
|
LL | fn lifetime_in_bounds<'a>(x: &'a ()) -> impl use<> Into<&'a ()> { x }
| -- -----------------^^----
| | |
| | lifetime captured due to being mentioned in the bounds of the `impl Trait`
| this lifetime parameter is captured
LL | fn type_param<T>() -> impl use<> Sized {}
| ^ ---------------- type parameter is implicitly captured by this `impl Trait`
|
= note: currently, all type parameters are required to be mentioned in the precise captures list
error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds
--> $DIR/forgot-to-capture-type.rs:7:60
error: `impl Trait` must mention all type parameters in scope
--> $DIR/forgot-to-capture-type.rs:7:1
|
LL | fn lifetime_in_hidden<'a>(x: &'a ()) -> impl use<> Sized { x }
| -- ---------------- ^
| | |
| | opaque type defined here
| hidden type `&'a ()` captures the lifetime `'a` as defined here
LL | trait Foo {
| ^^^^^^^^^
LL |
LL | fn bar() -> impl use<> Sized;
| ---------------- type parameter is implicitly captured by this `impl Trait`
|
help: to declare that `impl Sized` captures `'a`, you can add an explicit `'a` lifetime bound
|
LL | fn lifetime_in_hidden<'a>(x: &'a ()) -> impl use<> Sized + 'a { x }
| ++++
= note: currently, all type parameters are required to be mentioned in the precise captures list
error: aborting due to 2 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0700`.

View File

@ -0,0 +1,10 @@
//@ check-pass
#![feature(precise_capturing)]
//~^ WARN the feature `precise_capturing` is incomplete
trait Foo {
fn bar<'a>() -> impl use<Self> Sized;
}
fn main() {}

View File

@ -0,0 +1,11 @@
warning: the feature `precise_capturing` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/self-capture.rs:3:12
|
LL | #![feature(precise_capturing)]
| ^^^^^^^^^^^^^^^^^
|
= note: see issue #123432 <https://github.com/rust-lang/rust/issues/123432> for more information
= note: `#[warn(incomplete_features)]` on by default
warning: 1 warning emitted