Skip to content

Commit 5a6fe3f

Browse files
authored
Rollup merge of rust-lang#97720 - cjgillot:all-fresh, r=petrochenkov
Always create elided lifetime parameters for functions Anonymous and elided lifetimes in functions are sometimes (async fns) --and sometimes not (regular fns)-- desugared to implicit generic parameters. This difference of treatment makes it some downstream analyses more complicated to handle. This step is a pre-requisite to perform lifetime elision resolution on AST. There is currently an inconsistency in the treatment of argument-position impl-trait for functions and async fns: ```rust trait Foo<'a> {} fn foo(t: impl Foo<'_>) {} //~ ERROR missing lifetime specifier async fn async_foo(t: impl Foo<'_>) {} //~ OK fn bar(t: impl Iterator<Item = &'_ u8>) {} //~ ERROR missing lifetime specifier async fn async_bar(t: impl Iterator<Item = &'_ u8>) {} //~ OK ``` The current implementation reports "missing lifetime specifier" on `foo`, but **accepts it** in `async_foo`. This PR **proposes to accept** the anonymous lifetime in both cases as an extra generic lifetime parameter. This change would be insta-stable, so let's ping t-lang. Anonymous lifetimes in GAT bindings keep being forbidden: ```rust fn foo(t: impl Foo<Assoc<'_> = Bar<'_>>) {} ^^ ^^ forbidden ok ``` I started a discussion here: https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Anonymous.20lifetimes.20in.20universal.20impl-trait/near/284968606 r? ``@petrochenkov``
2 parents eee0bf4 + dcd2484 commit 5a6fe3f

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

clippy_lints/src/inherent_to_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
33
use clippy_utils::{get_trait_def_id, paths, return_ty, trait_ref_of_method};
44
use if_chain::if_chain;
5-
use rustc_hir::{ImplItem, ImplItemKind};
5+
use rustc_hir::{GenericParamKind, ImplItem, ImplItemKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88
use rustc_span::sym;
@@ -102,7 +102,7 @@ impl<'tcx> LateLintPass<'tcx> for InherentToString {
102102
let decl = &signature.decl;
103103
if decl.implicit_self.has_implicit_self();
104104
if decl.inputs.len() == 1;
105-
if impl_item.generics.params.is_empty();
105+
if impl_item.generics.params.iter().all(|p| matches!(p.kind, GenericParamKind::Lifetime { .. }));
106106

107107
// Check if return type is String
108108
if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::String);

clippy_lints/src/lifetimes.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use rustc_hir::intravisit::{
99
use rustc_hir::FnRetTy::Return;
1010
use rustc_hir::{
1111
BareFnTy, BodyId, FnDecl, GenericArg, GenericBound, GenericParam, GenericParamKind, Generics, Impl, ImplItem,
12-
ImplItemKind, Item, ItemKind, LangItem, Lifetime, LifetimeName, ParamName, PolyTraitRef, PredicateOrigin,
13-
TraitBoundModifier, TraitFn, TraitItem, TraitItemKind, Ty, TyKind, WherePredicate,
12+
ImplItemKind, Item, ItemKind, LangItem, Lifetime, LifetimeName, LifetimeParamKind, ParamName, PolyTraitRef,
13+
PredicateOrigin, TraitBoundModifier, TraitFn, TraitItem, TraitItemKind, Ty, TyKind, WherePredicate,
1414
};
1515
use rustc_lint::{LateContext, LateLintPass};
1616
use rustc_middle::hir::nested_filter as middle_nested_filter;
@@ -338,7 +338,10 @@ fn could_use_elision<'tcx>(
338338
fn allowed_lts_from(named_generics: &[GenericParam<'_>]) -> FxHashSet<RefLt> {
339339
let mut allowed_lts = FxHashSet::default();
340340
for par in named_generics.iter() {
341-
if let GenericParamKind::Lifetime { .. } = par.kind {
341+
if let GenericParamKind::Lifetime {
342+
kind: LifetimeParamKind::Explicit,
343+
} = par.kind
344+
{
342345
allowed_lts.insert(RefLt::Named(par.name.ident().name));
343346
}
344347
}
@@ -379,6 +382,7 @@ impl<'a, 'tcx> RefVisitor<'a, 'tcx> {
379382
self.lts.push(RefLt::Static);
380383
} else if let LifetimeName::Param(_, ParamName::Fresh) = lt.name {
381384
// Fresh lifetimes generated should be ignored.
385+
self.lts.push(RefLt::Unnamed);
382386
} else if lt.is_elided() {
383387
self.lts.push(RefLt::Unnamed);
384388
} else {

clippy_lints/src/ptr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,13 @@ fn check_mut_from_ref<'tcx>(cx: &LateContext<'tcx>, sig: &FnSig<'_>, body: Optio
495495
if let FnRetTy::Return(ty) = sig.decl.output
496496
&& let Some((out, Mutability::Mut, _)) = get_rptr_lm(ty)
497497
{
498+
let out_region = cx.tcx.named_region(out.hir_id);
498499
let args: Option<Vec<_>> = sig
499500
.decl
500501
.inputs
501502
.iter()
502503
.filter_map(get_rptr_lm)
503-
.filter(|&(lt, _, _)| lt.name == out.name)
504+
.filter(|&(lt, _, _)| cx.tcx.named_region(lt.hir_id) == out_region)
504505
.map(|(_, mutability, span)| (mutability == Mutability::Not).then(|| span))
505506
.collect();
506507
if let Some(args) = args

clippy_lints/src/types/borrowed_box.rs

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

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

0 commit comments

Comments
 (0)