Skip to content

Commit 696ea06

Browse files
committed
Auto merge of rust-lang#104048 - cjgillot:split-lifetime, r=compiler-errors
Separate lifetime ident from lifetime resolution in HIR Drive-by: change how suggested generic args are computed. Fixes rust-lang#103815 I recommend reviewing commit-by-commit.
2 parents 8b2f7e3 + 42db5e5 commit 696ea06

File tree

5 files changed

+33
-46
lines changed

5 files changed

+33
-46
lines changed

clippy_lints/src/lifetimes.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_hir::lang_items;
1010
use rustc_hir::FnRetTy::Return;
1111
use rustc_hir::{
1212
BareFnTy, BodyId, FnDecl, GenericArg, GenericBound, GenericParam, GenericParamKind, Generics, Impl, ImplItem,
13-
ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, ParamName, PolyTraitRef, PredicateOrigin, TraitFn, TraitItem,
14-
TraitItemKind, Ty, TyKind, WherePredicate,
13+
ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, LifetimeParamKind, PolyTraitRef, PredicateOrigin, TraitFn,
14+
TraitItem, TraitItemKind, Ty, TyKind, WherePredicate,
1515
};
1616
use rustc_lint::{LateContext, LateLintPass};
1717
use rustc_middle::hir::nested_filter as middle_nested_filter;
@@ -180,7 +180,7 @@ fn check_fn_inner<'tcx>(
180180
_ => None,
181181
});
182182
for bound in lifetimes {
183-
if bound.name != LifetimeName::Static && !bound.is_elided() {
183+
if !bound.is_static() && !bound.is_elided() {
184184
return;
185185
}
186186
}
@@ -414,17 +414,13 @@ impl<'a, 'tcx> RefVisitor<'a, 'tcx> {
414414

415415
fn record(&mut self, lifetime: &Option<Lifetime>) {
416416
if let Some(ref lt) = *lifetime {
417-
if lt.name == LifetimeName::Static {
417+
if lt.is_static() {
418418
self.lts.push(RefLt::Static);
419-
} else if let LifetimeName::Param(_, ParamName::Fresh) = lt.name {
419+
} else if lt.is_anonymous() {
420420
// Fresh lifetimes generated should be ignored.
421421
self.lts.push(RefLt::Unnamed);
422-
} else if lt.is_elided() {
423-
self.lts.push(RefLt::Unnamed);
424-
} else if let LifetimeName::Param(def_id, _) = lt.name {
422+
} else if let LifetimeName::Param(def_id) = lt.res {
425423
self.lts.push(RefLt::Named(def_id));
426-
} else {
427-
self.lts.push(RefLt::Unnamed);
428424
}
429425
} else {
430426
self.lts.push(RefLt::Unnamed);
@@ -472,7 +468,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> {
472468
walk_item(self, item);
473469
self.lts.truncate(len);
474470
self.lts.extend(bounds.iter().filter_map(|bound| match bound {
475-
GenericArg::Lifetime(l) => Some(if let LifetimeName::Param(def_id, _) = l.name {
471+
GenericArg::Lifetime(l) => Some(if let LifetimeName::Param(def_id) = l.res {
476472
RefLt::Named(def_id)
477473
} else {
478474
RefLt::Unnamed
@@ -498,10 +494,8 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> {
498494
}
499495

500496
fn visit_generic_arg(&mut self, generic_arg: &'tcx GenericArg<'tcx>) {
501-
if let GenericArg::Lifetime(l) = generic_arg
502-
&& let LifetimeName::Param(def_id, _) = l.name
503-
{
504-
self.lifetime_generic_arg_spans.entry(def_id).or_insert(l.span);
497+
if let GenericArg::Lifetime(l) = generic_arg && let LifetimeName::Param(def_id) = l.res {
498+
self.lifetime_generic_arg_spans.entry(def_id).or_insert(l.ident.span);
505499
}
506500
// Replace with `walk_generic_arg` if/when https://github.com/rust-lang/rust/pull/103692 lands.
507501
// walk_generic_arg(self, generic_arg);
@@ -577,7 +571,7 @@ where
577571

578572
// for lifetimes as parameters of generics
579573
fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) {
580-
self.map.remove(&lifetime.name.ident().name);
574+
self.map.remove(&lifetime.ident.name);
581575
}
582576

583577
fn visit_generic_param(&mut self, param: &'tcx GenericParam<'_>) {
@@ -601,7 +595,9 @@ fn report_extra_lifetimes<'tcx>(cx: &LateContext<'tcx>, func: &'tcx FnDecl<'_>,
601595
.params
602596
.iter()
603597
.filter_map(|par| match par.kind {
604-
GenericParamKind::Lifetime { .. } => Some((par.name.ident().name, par.span)),
598+
GenericParamKind::Lifetime {
599+
kind: LifetimeParamKind::Explicit,
600+
} => Some((par.name.ident().name, par.span)),
605601
_ => None,
606602
})
607603
.collect();
@@ -626,7 +622,9 @@ fn report_extra_impl_lifetimes<'tcx>(cx: &LateContext<'tcx>, impl_: &'tcx Impl<'
626622
.params
627623
.iter()
628624
.filter_map(|par| match par.kind {
629-
GenericParamKind::Lifetime { .. } => Some((par.name.ident().name, par.span)),
625+
GenericParamKind::Lifetime {
626+
kind: LifetimeParamKind::Explicit,
627+
} => Some((par.name.ident().name, par.span)),
630628
_ => None,
631629
})
632630
.collect();
@@ -653,7 +651,7 @@ struct BodyLifetimeChecker {
653651
impl<'tcx> Visitor<'tcx> for BodyLifetimeChecker {
654652
// for lifetimes as parameters of generics
655653
fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) {
656-
if lifetime.name.ident().name != kw::UnderscoreLifetime && lifetime.name.ident().name != kw::StaticLifetime {
654+
if !lifetime.is_anonymous() && lifetime.ident.name != kw::StaticLifetime {
657655
self.lifetimes_used_in_body = true;
658656
}
659657
}

clippy_lints/src/manual_async_fn.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fn future_trait_ref<'tcx>(
118118
.iter()
119119
.filter_map(|bound| {
120120
if let GenericArg::Lifetime(lt) = bound {
121-
Some(lt.name)
121+
Some(lt.res)
122122
} else {
123123
None
124124
}
@@ -153,7 +153,7 @@ fn captures_all_lifetimes(inputs: &[Ty<'_>], output_lifetimes: &[LifetimeName])
153153
.iter()
154154
.filter_map(|ty| {
155155
if let TyKind::Rptr(lt, _) = ty.kind {
156-
Some(lt.name)
156+
Some(lt.res)
157157
} else {
158158
None
159159
}

clippy_lints/src/ptr.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use rustc_hir::hir_id::HirIdMap;
1212
use rustc_hir::intravisit::{walk_expr, Visitor};
1313
use rustc_hir::{
1414
self as hir, AnonConst, BinOpKind, BindingAnnotation, Body, Expr, ExprKind, FnRetTy, FnSig, GenericArg,
15-
ImplItemKind, ItemKind, Lifetime, LifetimeName, Mutability, Node, Param, ParamName, PatKind, QPath, TraitFn,
16-
TraitItem, TraitItemKind, TyKind, Unsafety,
15+
ImplItemKind, ItemKind, Lifetime, Mutability, Node, Param, PatKind, QPath, TraitFn, TraitItem, TraitItemKind,
16+
TyKind, Unsafety,
1717
};
1818
use rustc_infer::infer::TyCtxtInferExt;
1919
use rustc_infer::traits::{Obligation, ObligationCause};
@@ -343,21 +343,16 @@ impl PtrArg<'_> {
343343
}
344344

345345
struct RefPrefix {
346-
lt: LifetimeName,
346+
lt: Lifetime,
347347
mutability: Mutability,
348348
}
349349
impl fmt::Display for RefPrefix {
350350
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
351351
use fmt::Write;
352352
f.write_char('&')?;
353-
match self.lt {
354-
LifetimeName::Param(_, ParamName::Plain(name)) => {
355-
name.fmt(f)?;
356-
f.write_char(' ')?;
357-
},
358-
LifetimeName::Infer => f.write_str("'_ ")?,
359-
LifetimeName::Static => f.write_str("'static ")?,
360-
_ => (),
353+
if !self.lt.is_anonymous() {
354+
self.lt.ident.fmt(f)?;
355+
f.write_char(' ')?;
361356
}
362357
f.write_str(self.mutability.prefix_str())
363358
}
@@ -495,7 +490,7 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
495490
ty_name: name.ident.name,
496491
method_renames,
497492
ref_prefix: RefPrefix {
498-
lt: lt.name,
493+
lt: lt.clone(),
499494
mutability,
500495
},
501496
deref_ty,

clippy_lints/src/types/borrowed_box.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m
3131
return false;
3232
}
3333

34-
let ltopt = if lt.name.is_anonymous() {
34+
let ltopt = if lt.is_anonymous() {
3535
String::new()
3636
} else {
37-
format!("{} ", lt.name.ident().as_str())
37+
format!("{} ", lt.ident.as_str())
3838
};
3939

4040
if mut_ty.mutbl == Mutability::Mut {

clippy_utils/src/hir_utils.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def::Res;
77
use rustc_hir::HirIdMap;
88
use rustc_hir::{
99
ArrayLen, BinOpKind, BindingAnnotation, Block, BodyId, Closure, Expr, ExprField, ExprKind, FnRetTy, GenericArg,
10-
GenericArgs, Guard, HirId, InlineAsmOperand, Let, Lifetime, LifetimeName, ParamName, Pat, PatField, PatKind, Path,
10+
GenericArgs, Guard, HirId, InlineAsmOperand, Let, Lifetime, LifetimeName, Pat, PatField, PatKind, Path,
1111
PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind, TypeBinding,
1212
};
1313
use rustc_lexer::{tokenize, TokenKind};
@@ -337,7 +337,7 @@ impl HirEqInterExpr<'_, '_, '_> {
337337
}
338338

339339
fn eq_lifetime(left: &Lifetime, right: &Lifetime) -> bool {
340-
left.name == right.name
340+
left.res == right.res
341341
}
342342

343343
fn eq_pat_field(&mut self, left: &PatField<'_>, right: &PatField<'_>) -> bool {
@@ -925,16 +925,10 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
925925
}
926926

927927
pub fn hash_lifetime(&mut self, lifetime: &Lifetime) {
928-
std::mem::discriminant(&lifetime.name).hash(&mut self.s);
929-
if let LifetimeName::Param(param_id, ref name) = lifetime.name {
930-
std::mem::discriminant(name).hash(&mut self.s);
928+
lifetime.ident.name.hash(&mut self.s);
929+
std::mem::discriminant(&lifetime.res).hash(&mut self.s);
930+
if let LifetimeName::Param(param_id) = lifetime.res {
931931
param_id.hash(&mut self.s);
932-
match name {
933-
ParamName::Plain(ref ident) => {
934-
ident.name.hash(&mut self.s);
935-
},
936-
ParamName::Fresh | ParamName::Error => {},
937-
}
938932
}
939933
}
940934

0 commit comments

Comments
 (0)