Skip to content

Commit 5974fe8

Browse files
committed
Auto merge of rust-lang#123725 - GuillaumeGomez:rollup-gk2bbrg, r=GuillaumeGomez
Rollup of 7 pull requests Successful merges: - rust-lang#118391 (Add `REDUNDANT_LIFETIMES` lint to detect lifetimes which are semantically redundant) - rust-lang#123534 (Windows: set main thread name without re-encoding) - rust-lang#123659 (Add support to intrinsics fallback body) - rust-lang#123689 (Add const generics support for pattern types) - rust-lang#123701 (Only assert for child/parent projection compatibility AFTER checking that theyre coming from the same place) - rust-lang#123702 (Further cleanup cfgs in the UI test suite) - rust-lang#123706 (rustdoc: reduce per-page HTML overhead) r? `@ghost` `@rustbot` modify labels: rollup
2 parents e908cfd + 96628f4 commit 5974fe8

File tree

50 files changed

+749
-236
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+749
-236
lines changed

compiler/rustc_errors/src/diagnostic_impls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl<'a, T: Clone + IntoDiagArg> IntoDiagArg for &'a T {
4646
}
4747
}
4848

49+
#[macro_export]
4950
macro_rules! into_diag_arg_using_display {
5051
($( $ty:ty ),+ $(,)?) => {
5152
$(

compiler/rustc_hir_analysis/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ hir_analysis_pattern_type_wild_pat = "wildcard patterns are not permitted for pa
355355
hir_analysis_placeholder_not_allowed_item_signatures = the placeholder `_` is not allowed within types on item signatures for {$kind}
356356
.label = not allowed in type signatures
357357
358+
hir_analysis_redundant_lifetime_args = unnecessary lifetime parameter `{$victim}`
359+
.note = you can use the `{$candidate}` lifetime directly, in place of `{$victim}`
360+
358361
hir_analysis_requires_note = the `{$trait_name}` impl for `{$ty}` requires that `{$error_predicate}`
359362
360363
hir_analysis_return_type_notation_equality_bound =

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+135
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ use rustc_ast as ast;
88
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
99
use rustc_errors::{codes::*, pluralize, struct_span_code_err, Applicability, ErrorGuaranteed};
1010
use rustc_hir as hir;
11+
use rustc_hir::def::DefKind;
1112
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
1213
use rustc_hir::lang_items::LangItem;
1314
use rustc_hir::ItemKind;
1415
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
1516
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
17+
use rustc_macros::LintDiagnostic;
1618
use rustc_middle::query::Providers;
1719
use rustc_middle::ty::print::with_no_trimmed_paths;
1820
use rustc_middle::ty::trait_def::TraitSpecializationKind;
@@ -136,6 +138,8 @@ where
136138
infcx.implied_bounds_tys_compat(param_env, body_def_id, &assumed_wf_types, false);
137139
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
138140

141+
lint_redundant_lifetimes(tcx, body_def_id, &outlives_env);
142+
139143
let errors = infcx.resolve_regions(&outlives_env);
140144
if errors.is_empty() {
141145
return Ok(());
@@ -2010,6 +2014,137 @@ fn check_mod_type_wf(tcx: TyCtxt<'_>, module: LocalModDefId) -> Result<(), Error
20102014
res
20112015
}
20122016

2017+
fn lint_redundant_lifetimes<'tcx>(
2018+
tcx: TyCtxt<'tcx>,
2019+
owner_id: LocalDefId,
2020+
outlives_env: &OutlivesEnvironment<'tcx>,
2021+
) {
2022+
let def_kind = tcx.def_kind(owner_id);
2023+
match def_kind {
2024+
DefKind::Struct
2025+
| DefKind::Union
2026+
| DefKind::Enum
2027+
| DefKind::Trait
2028+
| DefKind::TraitAlias
2029+
| DefKind::Fn
2030+
| DefKind::Const
2031+
| DefKind::Impl { of_trait: _ } => {
2032+
// Proceed
2033+
}
2034+
DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
2035+
let parent_def_id = tcx.local_parent(owner_id);
2036+
if matches!(tcx.def_kind(parent_def_id), DefKind::Impl { of_trait: true }) {
2037+
// Don't check for redundant lifetimes for associated items of trait
2038+
// implementations, since the signature is required to be compatible
2039+
// with the trait, even if the implementation implies some lifetimes
2040+
// are redundant.
2041+
return;
2042+
}
2043+
}
2044+
DefKind::Mod
2045+
| DefKind::Variant
2046+
| DefKind::TyAlias
2047+
| DefKind::ForeignTy
2048+
| DefKind::TyParam
2049+
| DefKind::ConstParam
2050+
| DefKind::Static { .. }
2051+
| DefKind::Ctor(_, _)
2052+
| DefKind::Macro(_)
2053+
| DefKind::ExternCrate
2054+
| DefKind::Use
2055+
| DefKind::ForeignMod
2056+
| DefKind::AnonConst
2057+
| DefKind::InlineConst
2058+
| DefKind::OpaqueTy
2059+
| DefKind::Field
2060+
| DefKind::LifetimeParam
2061+
| DefKind::GlobalAsm
2062+
| DefKind::Closure => return,
2063+
}
2064+
2065+
// The ordering of this lifetime map is a bit subtle.
2066+
//
2067+
// Specifically, we want to find a "candidate" lifetime that precedes a "victim" lifetime,
2068+
// where we can prove that `'candidate = 'victim`.
2069+
//
2070+
// `'static` must come first in this list because we can never replace `'static` with
2071+
// something else, but if we find some lifetime `'a` where `'a = 'static`, we want to
2072+
// suggest replacing `'a` with `'static`.
2073+
let mut lifetimes = vec![tcx.lifetimes.re_static];
2074+
lifetimes.extend(
2075+
ty::GenericArgs::identity_for_item(tcx, owner_id).iter().filter_map(|arg| arg.as_region()),
2076+
);
2077+
// If we are in a function, add its late-bound lifetimes too.
2078+
if matches!(def_kind, DefKind::Fn | DefKind::AssocFn) {
2079+
for var in tcx.fn_sig(owner_id).instantiate_identity().bound_vars() {
2080+
let ty::BoundVariableKind::Region(kind) = var else { continue };
2081+
lifetimes.push(ty::Region::new_late_param(tcx, owner_id.to_def_id(), kind));
2082+
}
2083+
}
2084+
lifetimes.retain(|candidate| candidate.has_name());
2085+
2086+
// Keep track of lifetimes which have already been replaced with other lifetimes.
2087+
// This makes sure that if `'a = 'b = 'c`, we don't say `'c` should be replaced by
2088+
// both `'a` and `'b`.
2089+
let mut shadowed = FxHashSet::default();
2090+
2091+
for (idx, &candidate) in lifetimes.iter().enumerate() {
2092+
// Don't suggest removing a lifetime twice. We only need to check this
2093+
// here and not up in the `victim` loop because equality is transitive,
2094+
// so if A = C and B = C, then A must = B, so it'll be shadowed too in
2095+
// A's victim loop.
2096+
if shadowed.contains(&candidate) {
2097+
continue;
2098+
}
2099+
2100+
for &victim in &lifetimes[(idx + 1)..] {
2101+
// We should only have late-bound lifetimes of the `BrNamed` variety,
2102+
// since we get these signatures straight from `hir_lowering`. And any
2103+
// other regions (ReError/ReStatic/etc.) shouldn't matter, since we
2104+
// can't really suggest to remove them.
2105+
let (ty::ReEarlyParam(ty::EarlyParamRegion { def_id, .. })
2106+
| ty::ReLateParam(ty::LateParamRegion {
2107+
bound_region: ty::BoundRegionKind::BrNamed(def_id, _),
2108+
..
2109+
})) = victim.kind()
2110+
else {
2111+
continue;
2112+
};
2113+
2114+
// Do not rename lifetimes not local to this item since they'll overlap
2115+
// with the lint running on the parent. We still want to consider parent
2116+
// lifetimes which make child lifetimes redundant, otherwise we would
2117+
// have truncated the `identity_for_item` args above.
2118+
if tcx.parent(def_id) != owner_id.to_def_id() {
2119+
continue;
2120+
}
2121+
2122+
// If `candidate <: victim` and `victim <: candidate`, then they're equal.
2123+
if outlives_env.free_region_map().sub_free_regions(tcx, candidate, victim)
2124+
&& outlives_env.free_region_map().sub_free_regions(tcx, victim, candidate)
2125+
{
2126+
shadowed.insert(victim);
2127+
tcx.emit_node_span_lint(
2128+
rustc_lint_defs::builtin::REDUNDANT_LIFETIMES,
2129+
tcx.local_def_id_to_hir_id(def_id.expect_local()),
2130+
tcx.def_span(def_id),
2131+
RedundantLifetimeArgsLint { candidate, victim },
2132+
);
2133+
}
2134+
}
2135+
}
2136+
}
2137+
2138+
#[derive(LintDiagnostic)]
2139+
#[diag(hir_analysis_redundant_lifetime_args)]
2140+
#[note]
2141+
struct RedundantLifetimeArgsLint<'tcx> {
2142+
/// The lifetime we have found to be redundant.
2143+
victim: ty::Region<'tcx>,
2144+
// The lifetime we can replace the victim with.
2145+
candidate: ty::Region<'tcx>,
2146+
}
2147+
20132148
pub fn provide(providers: &mut Providers) {
20142149
*providers = Providers { check_mod_type_wf, check_well_formed, ..*providers };
20152150
}

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

-26
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use rustc_middle::hir::nested_filter;
2020
use rustc_middle::middle::resolve_bound_vars::*;
2121
use rustc_middle::query::Providers;
2222
use rustc_middle::ty::{self, TyCtxt, TypeSuperVisitable, TypeVisitor};
23-
use rustc_session::lint;
2423
use rustc_span::def_id::DefId;
2524
use rustc_span::symbol::{sym, Ident};
2625
use rustc_span::Span;
@@ -867,31 +866,6 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
867866
}) => {
868867
self.visit_lifetime(lifetime);
869868
walk_list!(self, visit_param_bound, bounds);
870-
871-
if lifetime.res != hir::LifetimeName::Static {
872-
for bound in bounds {
873-
let hir::GenericBound::Outlives(lt) = bound else {
874-
continue;
875-
};
876-
if lt.res != hir::LifetimeName::Static {
877-
continue;
878-
}
879-
self.insert_lifetime(lt, ResolvedArg::StaticLifetime);
880-
self.tcx.node_span_lint(
881-
lint::builtin::UNUSED_LIFETIMES,
882-
lifetime.hir_id,
883-
lifetime.ident.span,
884-
format!("unnecessary lifetime parameter `{}`", lifetime.ident),
885-
|lint| {
886-
let help = format!(
887-
"you can use the `'static` lifetime directly, in place of `{}`",
888-
lifetime.ident,
889-
);
890-
lint.help(help);
891-
},
892-
);
893-
}
894-
}
895869
}
896870
&hir::WherePredicate::EqPredicate(hir::WhereEqPredicate { lhs_ty, rhs_ty, .. }) => {
897871
self.visit_ty(lhs_ty);

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,24 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
22232223
Err(LitToConstError::TypeError) => todo!(),
22242224
}
22252225
}
2226+
2227+
hir::ExprKind::Path(hir::QPath::Resolved(
2228+
_,
2229+
&hir::Path {
2230+
res: Res::Def(DefKind::ConstParam, def_id), ..
2231+
},
2232+
)) => {
2233+
let ty = tcx
2234+
.type_of(def_id)
2235+
.no_bound_vars()
2236+
.expect("const parameter types cannot be generic");
2237+
let item_def_id = tcx.parent(def_id);
2238+
let generics = tcx.generics_of(item_def_id);
2239+
let index = generics.param_def_id_to_index[&def_id];
2240+
let name = tcx.item_name(def_id);
2241+
ty::Const::new_param(tcx, ty::ParamConst::new(index, name), ty)
2242+
}
2243+
22262244
_ => {
22272245
let err = tcx
22282246
.dcx()

compiler/rustc_lint_defs/src/builtin.rs

+28
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ declare_lint_pass! {
7979
PROC_MACRO_BACK_COMPAT,
8080
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
8181
PUB_USE_OF_PRIVATE_EXTERN_CRATE,
82+
REDUNDANT_LIFETIMES,
8283
REFINING_IMPL_TRAIT_INTERNAL,
8384
REFINING_IMPL_TRAIT_REACHABLE,
8485
RENAMED_AND_REMOVED_LINTS,
@@ -1707,6 +1708,33 @@ declare_lint! {
17071708
"detects lifetime parameters that are never used"
17081709
}
17091710

1711+
declare_lint! {
1712+
/// The `redundant_lifetimes` lint detects lifetime parameters that are
1713+
/// redundant because they are equal to another named lifetime.
1714+
///
1715+
/// ### Example
1716+
///
1717+
/// ```rust,compile_fail
1718+
/// #[deny(redundant_lifetimes)]
1719+
///
1720+
/// // `'a = 'static`, so all usages of `'a` can be replaced with `'static`
1721+
/// pub fn bar<'a: 'static>() {}
1722+
///
1723+
/// // `'a = 'b`, so all usages of `'b` can be replaced with `'a`
1724+
/// pub fn bar<'a: 'b, 'b: 'a>() {}
1725+
/// ```
1726+
///
1727+
/// {{produces}}
1728+
///
1729+
/// ### Explanation
1730+
///
1731+
/// Unused lifetime parameters may signal a mistake or unfinished code.
1732+
/// Consider removing the parameter.
1733+
pub REDUNDANT_LIFETIMES,
1734+
Allow,
1735+
"detects lifetime parameters that are redundant because they are equal to some other named lifetime"
1736+
}
1737+
17101738
declare_lint! {
17111739
/// The `tyvar_behind_raw_pointer` lint detects raw pointer to an
17121740
/// inference variable.

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ pub struct GlobalCtxt<'tcx> {
757757
impl<'tcx> GlobalCtxt<'tcx> {
758758
/// Installs `self` in a `TyCtxt` and `ImplicitCtxt` for the duration of
759759
/// `f`.
760-
pub fn enter<'a: 'tcx, F, R>(&'a self, f: F) -> R
760+
pub fn enter<F, R>(&'tcx self, f: F) -> R
761761
where
762762
F: FnOnce(TyCtxt<'tcx>) -> R,
763763
{

compiler/rustc_middle/src/ty/diagnostics.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@ use std::fmt::Write;
55
use std::ops::ControlFlow;
66

77
use crate::ty::{
8-
AliasTy, Const, ConstKind, FallibleTypeFolder, InferConst, InferTy, Opaque, PolyTraitPredicate,
9-
Projection, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable,
10-
TypeVisitor,
8+
self, AliasTy, Const, ConstKind, FallibleTypeFolder, InferConst, InferTy, Opaque,
9+
PolyTraitPredicate, Projection, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable,
10+
TypeSuperVisitable, TypeVisitable, TypeVisitor,
1111
};
1212

1313
use rustc_data_structures::fx::FxHashMap;
14-
use rustc_errors::{Applicability, Diag, DiagArgValue, IntoDiagArg};
14+
use rustc_errors::{into_diag_arg_using_display, Applicability, Diag, DiagArgValue, IntoDiagArg};
1515
use rustc_hir as hir;
1616
use rustc_hir::def::DefKind;
1717
use rustc_hir::def_id::DefId;
1818
use rustc_hir::{PredicateOrigin, WherePredicate};
1919
use rustc_span::{BytePos, Span};
2020
use rustc_type_ir::TyKind::*;
2121

22-
impl<'tcx> IntoDiagArg for Ty<'tcx> {
23-
fn into_diag_arg(self) -> DiagArgValue {
24-
self.to_string().into_diag_arg()
25-
}
22+
into_diag_arg_using_display! {
23+
Ty<'_>,
24+
ty::Region<'_>,
2625
}
2726

2827
impl<'tcx> Ty<'tcx> {

compiler/rustc_mir_transform/src/coroutine/by_move_body.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ impl<'tcx> MirPass<'tcx> for ByMoveBody {
154154
}) {
155155
let (child_field_idx, child_capture) = child_captures.next().unwrap();
156156

157+
// This analysis only makes sense if the parent capture is a
158+
// prefix of the child capture.
159+
assert!(
160+
child_capture.place.projections.len() >= parent_capture.place.projections.len(),
161+
"parent capture ({parent_capture:#?}) expected to be prefix of \
162+
child capture ({child_capture:#?})"
163+
);
164+
157165
// Store this set of additional projections (fields and derefs).
158166
// We need to re-apply them later.
159167
let child_precise_captures =
@@ -244,7 +252,6 @@ fn child_prefix_matches_parent_projections(
244252
bug!("expected capture to be an upvar");
245253
};
246254

247-
assert!(child_capture.place.projections.len() >= parent_capture.place.projections.len());
248255
parent_base.var_path.hir_id == child_base.var_path.hir_id
249256
&& std::iter::zip(&child_capture.place.projections, &parent_capture.place.projections)
250257
.all(|(child, parent)| child.kind == parent.kind)

0 commit comments

Comments
 (0)