Only look for param in generics if it actually comes from generics

This commit is contained in:
Michael Goulet 2023-02-28 06:26:48 +00:00
parent b583ede652
commit f851a8aefa
6 changed files with 117 additions and 47 deletions

View File

@ -1339,13 +1339,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
.map(|predicate| self.lower_where_predicate(predicate)), .map(|predicate| self.lower_where_predicate(predicate)),
); );
let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> = let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> = self
self.lower_generic_params_mut(&generics.params).collect(); .lower_generic_params_mut(&generics.params, hir::GenericParamSource::Generics)
.collect();
// Introduce extra lifetimes if late resolution tells us to. // Introduce extra lifetimes if late resolution tells us to.
let extra_lifetimes = self.resolver.take_extra_lifetime_params(parent_node_id); let extra_lifetimes = self.resolver.take_extra_lifetime_params(parent_node_id);
params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| { params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
self.lifetime_res_to_generic_param(ident, node_id, res) self.lifetime_res_to_generic_param(
ident,
node_id,
res,
hir::GenericParamSource::Generics,
)
})); }));
let has_where_clause_predicates = !generics.where_clause.predicates.is_empty(); let has_where_clause_predicates = !generics.where_clause.predicates.is_empty();
@ -1449,7 +1455,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
span, span,
}) => hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate { }) => hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
hir_id: self.next_id(), hir_id: self.next_id(),
bound_generic_params: self.lower_generic_params(bound_generic_params), bound_generic_params: self
.lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder),
bounded_ty: self bounded_ty: self
.lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)), .lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| { bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| {

View File

@ -804,6 +804,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
ident: Ident, ident: Ident,
node_id: NodeId, node_id: NodeId,
res: LifetimeRes, res: LifetimeRes,
source: hir::GenericParamSource,
) -> Option<hir::GenericParam<'hir>> { ) -> Option<hir::GenericParam<'hir>> {
let (name, kind) = match res { let (name, kind) = match res {
LifetimeRes::Param { .. } => { LifetimeRes::Param { .. } => {
@ -837,6 +838,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
pure_wrt_drop: false, pure_wrt_drop: false,
kind: hir::GenericParamKind::Lifetime { kind }, kind: hir::GenericParamKind::Lifetime { kind },
colon_span: None, colon_span: None,
source,
}) })
} }
@ -852,11 +854,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
binder: NodeId, binder: NodeId,
generic_params: &[GenericParam], generic_params: &[GenericParam],
) -> &'hir [hir::GenericParam<'hir>] { ) -> &'hir [hir::GenericParam<'hir>] {
let mut generic_params: Vec<_> = self.lower_generic_params_mut(generic_params).collect(); let mut generic_params: Vec<_> = self
.lower_generic_params_mut(generic_params, hir::GenericParamSource::Binder)
.collect();
let extra_lifetimes = self.resolver.take_extra_lifetime_params(binder); let extra_lifetimes = self.resolver.take_extra_lifetime_params(binder);
debug!(?extra_lifetimes); debug!(?extra_lifetimes);
generic_params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| { generic_params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
self.lifetime_res_to_generic_param(ident, node_id, res) self.lifetime_res_to_generic_param(ident, node_id, res, hir::GenericParamSource::Binder)
})); }));
let generic_params = self.arena.alloc_from_iter(generic_params); let generic_params = self.arena.alloc_from_iter(generic_params);
debug!(?generic_params); debug!(?generic_params);
@ -1375,8 +1379,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
span, span,
); );
let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span); let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span);
let (param, bounds, path) = let (param, bounds, path) = self.lower_universal_param_and_bounds(
self.lower_generic_and_bounds(*def_node_id, span, ident, bounds); *def_node_id,
span,
ident,
bounds,
);
self.impl_trait_defs.push(param); self.impl_trait_defs.push(param);
if let Some(bounds) = bounds { if let Some(bounds) = bounds {
self.impl_trait_bounds.push(bounds); self.impl_trait_bounds.push(bounds);
@ -1530,6 +1538,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
pure_wrt_drop: false, pure_wrt_drop: false,
kind: hir::GenericParamKind::Lifetime { kind }, kind: hir::GenericParamKind::Lifetime { kind },
colon_span: None, colon_span: None,
source: hir::GenericParamSource::Generics,
} }
}, },
)); ));
@ -1987,6 +1996,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
pure_wrt_drop: false, pure_wrt_drop: false,
kind: hir::GenericParamKind::Lifetime { kind }, kind: hir::GenericParamKind::Lifetime { kind },
colon_span: None, colon_span: None,
source: hir::GenericParamSource::Generics,
} }
}, },
)); ));
@ -2152,16 +2162,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn lower_generic_params_mut<'s>( fn lower_generic_params_mut<'s>(
&'s mut self, &'s mut self,
params: &'s [GenericParam], params: &'s [GenericParam],
source: hir::GenericParamSource,
) -> impl Iterator<Item = hir::GenericParam<'hir>> + Captures<'a> + Captures<'s> { ) -> impl Iterator<Item = hir::GenericParam<'hir>> + Captures<'a> + Captures<'s> {
params.iter().map(move |param| self.lower_generic_param(param)) params.iter().map(move |param| self.lower_generic_param(param, source))
} }
fn lower_generic_params(&mut self, params: &[GenericParam]) -> &'hir [hir::GenericParam<'hir>] { fn lower_generic_params(
self.arena.alloc_from_iter(self.lower_generic_params_mut(params)) &mut self,
params: &[GenericParam],
source: hir::GenericParamSource,
) -> &'hir [hir::GenericParam<'hir>] {
self.arena.alloc_from_iter(self.lower_generic_params_mut(params, source))
} }
#[instrument(level = "trace", skip(self))] #[instrument(level = "trace", skip(self))]
fn lower_generic_param(&mut self, param: &GenericParam) -> hir::GenericParam<'hir> { fn lower_generic_param(
&mut self,
param: &GenericParam,
source: hir::GenericParamSource,
) -> hir::GenericParam<'hir> {
let (name, kind) = self.lower_generic_param_kind(param); let (name, kind) = self.lower_generic_param_kind(param);
let hir_id = self.lower_node_id(param.id); let hir_id = self.lower_node_id(param.id);
@ -2174,6 +2193,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
pure_wrt_drop: self.tcx.sess.contains_name(&param.attrs, sym::may_dangle), pure_wrt_drop: self.tcx.sess.contains_name(&param.attrs, sym::may_dangle),
kind, kind,
colon_span: param.colon_span.map(|s| self.lower_span(s)), colon_span: param.colon_span.map(|s| self.lower_span(s)),
source,
} }
} }
@ -2266,7 +2286,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
} }
#[instrument(level = "debug", skip(self), ret)] #[instrument(level = "debug", skip(self), ret)]
fn lower_generic_and_bounds( fn lower_universal_param_and_bounds(
&mut self, &mut self,
node_id: NodeId, node_id: NodeId,
span: Span, span: Span,
@ -2286,6 +2306,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
span, span,
kind: hir::GenericParamKind::Type { default: None, synthetic: true }, kind: hir::GenericParamKind::Type { default: None, synthetic: true },
colon_span: None, colon_span: None,
source: hir::GenericParamSource::Generics,
}; };
let preds = self.lower_generic_bound_predicate( let preds = self.lower_generic_bound_predicate(

View File

@ -498,6 +498,7 @@ pub struct GenericParam<'hir> {
pub pure_wrt_drop: bool, pub pure_wrt_drop: bool,
pub kind: GenericParamKind<'hir>, pub kind: GenericParamKind<'hir>,
pub colon_span: Option<Span>, pub colon_span: Option<Span>,
pub source: GenericParamSource,
} }
impl<'hir> GenericParam<'hir> { impl<'hir> GenericParam<'hir> {
@ -516,6 +517,20 @@ impl<'hir> GenericParam<'hir> {
} }
} }
/// Records where the generic parameter originated from.
///
/// This can either be from an item's generics, in which case it's typically
/// early-bound (but can be a late-bound lifetime in functions, for example),
/// or from a `for<...>` binder, in which case it's late-bound (and notably,
/// does not show up in the parent item's generics).
#[derive(Debug, HashStable_Generic, PartialEq, Eq, Copy, Clone)]
pub enum GenericParamSource {
// Early or late-bound parameters defined on an item
Generics,
// Late-bound parameters defined via a `for<...>`
Binder,
}
#[derive(Default)] #[derive(Default)]
pub struct GenericParamCount { pub struct GenericParamCount {
pub lifetimes: usize, pub lifetimes: usize,

View File

@ -1034,6 +1034,11 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
fn object_lifetime_default(tcx: TyCtxt<'_>, param_def_id: DefId) -> ObjectLifetimeDefault { fn object_lifetime_default(tcx: TyCtxt<'_>, param_def_id: DefId) -> ObjectLifetimeDefault {
debug_assert_eq!(tcx.def_kind(param_def_id), DefKind::TyParam); debug_assert_eq!(tcx.def_kind(param_def_id), DefKind::TyParam);
let param_def_id = param_def_id.expect_local(); let param_def_id = param_def_id.expect_local();
let hir::Node::GenericParam(param) = tcx.hir().get_by_def_id(param_def_id) else {
bug!("expected GenericParam for object_lifetime_default");
};
match param.source {
hir::GenericParamSource::Generics => {
let parent_def_id = tcx.local_parent(param_def_id); let parent_def_id = tcx.local_parent(param_def_id);
let generics = tcx.hir().get_generics(parent_def_id).unwrap(); let generics = tcx.hir().get_generics(parent_def_id).unwrap();
let param_hir_id = tcx.local_def_id_to_hir_id(param_def_id); let param_hir_id = tcx.local_def_id_to_hir_id(param_def_id);
@ -1075,6 +1080,9 @@ fn object_lifetime_default(tcx: TyCtxt<'_>, param_def_id: DefId) -> ObjectLifeti
} }
} }
} }
hir::GenericParamSource::Binder => ObjectLifetimeDefault::Empty,
}
}
impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
fn with<F>(&mut self, wrap_scope: Scope<'_>, f: F) fn with<F>(&mut self, wrap_scope: Scope<'_>, f: F)
@ -1392,9 +1400,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
return; return;
} }
self.tcx self.tcx.sess.delay_span_bug(
.sess self.tcx.hir().span(hir_id),
.delay_span_bug(self.tcx.hir().span(hir_id), "could not resolve {param_def_id:?}"); format!("could not resolve {param_def_id:?}"),
);
} }
#[instrument(level = "debug", skip(self))] #[instrument(level = "debug", skip(self))]

View File

@ -0,0 +1,7 @@
// check-pass
// compile-flags: --crate-type=lib
#![feature(non_lifetime_binders)]
//~^ WARN the feature `non_lifetime_binders` is incomplete
pub fn f<T>() where for<U> (T, U): Copy {}

View File

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