diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 978186f76a1f0..7cc42260906e1 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -1364,7 +1364,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { } // Try to find predicates on *generic params* that would allow copying `ty` let mut suggestion = - if let Some(symbol) = tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) { + if let Some(symbol) = tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) { format!(": {symbol}.clone()") } else { ".clone()".to_owned() diff --git a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs index 29cc749877b3e..0394a42ea9c77 100644 --- a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs @@ -502,7 +502,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { let upvar = &self.upvars[upvar_field.unwrap().index()]; let upvar_hir_id = upvar.get_root_variable(); let upvar_name = upvar.to_string(tcx); - let upvar_span = tcx.hir().span(upvar_hir_id); + let upvar_span = tcx.hir_span(upvar_hir_id); let place_name = self.describe_any_place(move_place.as_ref()); diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index 50a18b04de4ef..d1d783c22e353 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -628,7 +628,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { if let Some(def_hir) = defined_hir { let upvars_map = self.infcx.tcx.upvars_mentioned(def_id).unwrap(); - let upvar_def_span = self.infcx.tcx.hir().span(def_hir); + let upvar_def_span = self.infcx.tcx.hir_span(def_hir); let upvar_span = upvars_map.get(&def_hir).unwrap().span; diag.subdiagnostic(VarHereDenote::Defined { span: upvar_def_span }); diag.subdiagnostic(VarHereDenote::Captured { span: upvar_span }); diff --git a/compiler/rustc_borrowck/src/diagnostics/region_name.rs b/compiler/rustc_borrowck/src/diagnostics/region_name.rs index 412aaf70c3f19..45f5eaa514b2a 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_name.rs @@ -291,7 +291,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> { match *error_region { ty::ReEarlyParam(ebr) => ebr.has_name().then(|| { let def_id = tcx.generics_of(self.mir_def_id()).region_param(ebr, tcx).def_id; - let span = tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP); + let span = tcx.hir_span_if_local(def_id).unwrap_or(DUMMY_SP); RegionName { name: ebr.name, source: RegionNameSource::NamedEarlyParamRegion(span) } }), @@ -302,7 +302,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> { ty::ReLateParam(late_param) => match late_param.kind { ty::LateParamRegionKind::Named(region_def_id, name) => { // Get the span to point to, even if we don't use the name. - let span = tcx.hir().span_if_local(region_def_id).unwrap_or(DUMMY_SP); + let span = tcx.hir_span_if_local(region_def_id).unwrap_or(DUMMY_SP); debug!( "bound region named: {:?}, is_named: {:?}", name, diff --git a/compiler/rustc_borrowck/src/diagnostics/var_name.rs b/compiler/rustc_borrowck/src/diagnostics/var_name.rs index 693d22abbe6c8..14ed6a27a7a15 100644 --- a/compiler/rustc_borrowck/src/diagnostics/var_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/var_name.rs @@ -70,7 +70,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { debug!("get_upvar_name_and_span_for_region: upvar_hir_id={upvar_hir_id:?}"); let upvar_name = tcx.hir_name(upvar_hir_id); - let upvar_span = tcx.hir().span(upvar_hir_id); + let upvar_span = tcx.hir_span(upvar_hir_id); debug!( "get_upvar_name_and_span_for_region: upvar_name={upvar_name:?} upvar_span={upvar_span:?}", ); diff --git a/compiler/rustc_const_eval/src/const_eval/valtrees.rs b/compiler/rustc_const_eval/src/const_eval/valtrees.rs index 3776fb55c2e5f..34239ae1d1527 100644 --- a/compiler/rustc_const_eval/src/const_eval/valtrees.rs +++ b/compiler/rustc_const_eval/src/const_eval/valtrees.rs @@ -256,7 +256,7 @@ pub(crate) fn eval_to_valtree<'tcx>( Err(err) => { let did = cid.instance.def_id(); let global_const_id = cid.display(tcx); - let span = tcx.hir().span_if_local(did); + let span = tcx.hir_span_if_local(did); match err { ValTreeCreationError::NodesOverflow => { let handled = diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 37755e7d61db1..f1dc4bb795e51 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -691,6 +691,34 @@ fn print_crate_info( }; println_info!("{}", passes::get_crate_name(sess, attrs)); } + CrateRootLintLevels => { + let Some(attrs) = attrs.as_ref() else { + // no crate attributes, print out an error and exit + return Compilation::Continue; + }; + let crate_name = passes::get_crate_name(sess, attrs); + let lint_store = crate::unerased_lint_store(sess); + let registered_tools = rustc_resolve::registered_tools_ast(sess.dcx(), attrs); + let features = rustc_expand::config::features(sess, attrs, crate_name); + let lint_levels = rustc_lint::LintLevelsBuilder::crate_root( + sess, + &features, + true, + lint_store, + ®istered_tools, + attrs, + ); + for lint in lint_store.get_lints() { + if let Some(feature_symbol) = lint.feature_gate + && !features.enabled(feature_symbol) + { + // lint is unstable and feature gate isn't active, don't print + continue; + } + let level = lint_levels.lint_level(lint).0; + println_info!("{}={}", lint.name_lower(), level.as_str()); + } + } Cfg => { let mut cfgs = sess .psess diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index e3e96894ed1f7..f8af1b81ca9be 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -2123,7 +2123,7 @@ pub type Lit = Spanned; /// explicit discriminant values for enum variants. /// /// You can check if this anon const is a default in a const param -/// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_def_id(..)` +/// `const N: usize = { ... }` with `tcx.hir_opt_const_param_default_param_def_id(..)` #[derive(Copy, Clone, Debug, HashStable_Generic)] pub struct AnonConst { #[stable_hasher(ignore)] diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 07b5837bd871d..a079865676304 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -578,10 +578,8 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe } } _ => { - tcx.dcx().span_delayed_bug( - tcx.hir().span(hir_id), - "parameter should have been resolved", - ); + tcx.dcx() + .span_delayed_bug(tcx.hir_span(hir_id), "parameter should have been resolved"); } } } @@ -1049,7 +1047,7 @@ fn check_impl_items_against_trait<'tcx>( leaf_def.as_ref().is_some_and(|node_item| !node_item.defining_node.is_from_trait()); if !is_implemented_here { - let full_impl_span = tcx.hir().span_with_body(tcx.local_def_id_to_hir_id(impl_id)); + let full_impl_span = tcx.hir_span_with_body(tcx.local_def_id_to_hir_id(impl_id)); match tcx.eval_default_body_stability(trait_item_id, full_impl_span) { EvalResult::Deny { feature, reason, issue, .. } => default_body_is_unstable( tcx, @@ -1105,7 +1103,7 @@ fn check_impl_items_against_trait<'tcx>( } if !missing_items.is_empty() { - let full_impl_span = tcx.hir().span_with_body(tcx.local_def_id_to_hir_id(impl_id)); + let full_impl_span = tcx.hir_span_with_body(tcx.local_def_id_to_hir_id(impl_id)); missing_items_err(tcx, impl_id, &missing_items, full_impl_span); } @@ -1321,7 +1319,7 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>) let typing_env = ty::TypingEnv::non_body_analysis(tcx, field.did); let layout = tcx.layout_of(typing_env.as_query_input(ty)); // We are currently checking the type this field came from, so it must be local - let span = tcx.hir().span_if_local(field.did).unwrap(); + let span = tcx.hir_span_if_local(field.did).unwrap(); let trivial = layout.is_ok_and(|layout| layout.is_1zst()); if !trivial { return (span, trivial, None); diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index 84d07c711fa40..aec9518494c65 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -1208,7 +1208,7 @@ fn extract_spans_for_error_reporting<'tcx>( TypeError::ArgumentMutability(i) | TypeError::ArgumentSorts(ExpectedFound { .. }, i) => { (impl_args.nth(i).unwrap(), trait_args.and_then(|mut args| args.nth(i))) } - _ => (cause.span, tcx.hir().span_if_local(trait_m.def_id)), + _ => (cause.span, tcx.hir_span_if_local(trait_m.def_id)), } } @@ -1261,7 +1261,7 @@ fn compare_self_type<'tcx>( self_descr ); err.span_label(impl_m_span, format!("`{self_descr}` used in impl")); - if let Some(span) = tcx.hir().span_if_local(trait_m.def_id) { + if let Some(span) = tcx.hir_span_if_local(trait_m.def_id) { err.span_label(span, format!("trait method declared without `{self_descr}`")); } else { err.note_trait_signature(trait_m.name, trait_m.signature(tcx)); @@ -1281,7 +1281,7 @@ fn compare_self_type<'tcx>( self_descr ); err.span_label(impl_m_span, format!("expected `{self_descr}` in impl")); - if let Some(span) = tcx.hir().span_if_local(trait_m.def_id) { + if let Some(span) = tcx.hir_span_if_local(trait_m.def_id) { err.span_label(span, format!("`{self_descr}` used in trait")); } else { err.note_trait_signature(trait_m.name, trait_m.signature(tcx)); @@ -1389,7 +1389,7 @@ fn compare_number_of_generics<'tcx>( .collect(); (Some(arg_spans), impl_trait_spans) } else { - let trait_span = tcx.hir().span_if_local(trait_.def_id); + let trait_span = tcx.hir_span_if_local(trait_.def_id); (trait_span.map(|s| vec![s]), vec![]) }; @@ -1481,7 +1481,7 @@ fn compare_number_of_method_arguments<'tcx>( } }) }) - .or_else(|| tcx.hir().span_if_local(trait_m.def_id)); + .or_else(|| tcx.hir_span_if_local(trait_m.def_id)); let (impl_m_sig, _) = &tcx.hir_expect_impl_item(impl_m.def_id.expect_local()).expect_fn(); let pos = impl_number_args.saturating_sub(1); @@ -2366,7 +2366,7 @@ fn try_report_async_mismatch<'tcx>( return Err(tcx.sess.dcx().emit_err(MethodShouldReturnFuture { span: tcx.def_span(impl_m.def_id), method_name: tcx.item_ident(impl_m.def_id), - trait_item_span: tcx.hir().span_if_local(trait_m.def_id), + trait_item_span: tcx.hir_span_if_local(trait_m.def_id), })); } } diff --git a/compiler/rustc_hir_analysis/src/check/mod.rs b/compiler/rustc_hir_analysis/src/check/mod.rs index d8ae421452759..38ed83886ac57 100644 --- a/compiler/rustc_hir_analysis/src/check/mod.rs +++ b/compiler/rustc_hir_analysis/src/check/mod.rs @@ -243,7 +243,7 @@ fn missing_items_err( tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity(), )); let code = format!("{padding}{snippet}\n{padding}"); - if let Some(span) = tcx.hir().span_if_local(trait_item.def_id) { + if let Some(span) = tcx.hir_span_if_local(trait_item.def_id) { missing_trait_item_label .push(errors::MissingTraitItemLabel { span, item: trait_item.name }); missing_trait_item.push(errors::MissingTraitItemSuggestion { @@ -534,7 +534,7 @@ fn bad_variant_count<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>, sp: Span, d let variant_spans: Vec<_> = adt .variants() .iter() - .map(|variant| tcx.hir().span_if_local(variant.def_id).unwrap()) + .map(|variant| tcx.hir_span_if_local(variant.def_id).unwrap()) .collect(); let (mut spans, mut many) = (Vec::new(), None); if let [start @ .., end] = &*variant_spans { diff --git a/compiler/rustc_hir_analysis/src/collect/generics_of.rs b/compiler/rustc_hir_analysis/src/collect/generics_of.rs index a153ce8ea902d..2bed28d7b710c 100644 --- a/compiler/rustc_hir_analysis/src/collect/generics_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/generics_of.rs @@ -116,8 +116,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics { { // enum variant discriminants are not allowed to use any kind of generics None - } else if let Some(param_id) = - tcx.hir().opt_const_param_default_param_def_id(hir_id) + } else if let Some(param_id) = tcx.hir_opt_const_param_default_param_def_id(hir_id) { // If the def_id we are calling generics_of on is an anon ct default i.e: // diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index 4bd89861a9e5a..98da8449d5e45 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -508,7 +508,7 @@ pub(super) fn explicit_predicates_of<'tcx>( if matches!(def_kind, DefKind::AnonConst) && tcx.features().generic_const_exprs() && let Some(defaulted_param_def_id) = - tcx.hir().opt_const_param_default_param_def_id(tcx.local_def_id_to_hir_id(def_id)) + tcx.hir_opt_const_param_default_param_def_id(tcx.local_def_id_to_hir_id(def_id)) { // In `generics_of` we set the generics' parent to be our parent's parent which means that // we lose out on the predicates of our actual parent if we dont return those predicates here. diff --git a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs index 9b0d57bd75b11..404753875ee5e 100644 --- a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs +++ b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs @@ -1529,7 +1529,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { if let ResolvedArg::LateBound(..) = def && let Some(what) = crossed_late_boundary { - let use_span = self.tcx.hir().span(hir_id); + let use_span = self.tcx.hir_span(hir_id); let def_span = self.tcx.def_span(param_def_id); let guar = match self.tcx.def_kind(param_def_id) { DefKind::ConstParam => { @@ -1576,11 +1576,11 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { } => { let guar = self.tcx.dcx().emit_err(match self.tcx.def_kind(param_def_id) { DefKind::TyParam => errors::LateBoundInApit::Type { - span: self.tcx.hir().span(hir_id), + span: self.tcx.hir_span(hir_id), param_span: self.tcx.def_span(param_def_id), }, DefKind::ConstParam => errors::LateBoundInApit::Const { - span: self.tcx.hir().span(hir_id), + span: self.tcx.hir_span(hir_id), param_span: self.tcx.def_span(param_def_id), }, kind => { @@ -1605,7 +1605,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { self.tcx .dcx() - .span_bug(self.tcx.hir().span(hir_id), format!("could not resolve {param_def_id:?}")); + .span_bug(self.tcx.hir_span(hir_id), format!("could not resolve {param_def_id:?}")); } #[instrument(level = "debug", skip(self))] diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs index f0dffd780bcc7..302abc27324da 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs @@ -684,7 +684,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { return Err(self.dcx().emit_err(crate::errors::ReturnTypeNotationOnNonRpitit { span: path_span, ty: tcx.liberate_late_bound_regions(item_def_id, output), - fn_span: tcx.hir().span_if_local(item_def_id), + fn_span: tcx.hir_span_if_local(item_def_id), note: (), })); }; diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs index 170500c7a1628..ecb453bced0a7 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs @@ -32,7 +32,7 @@ pub(crate) fn validate_cmse_abi<'tcx>( span, .. }) => *span, - _ => tcx.hir().span(hir_id), + _ => tcx.hir_span(hir_id), }; struct_span_code_err!( tcx.dcx(), diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs index 3b007c7719880..6e9c178d33a64 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs @@ -882,7 +882,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { let rename_message = if is_shadowed { ", consider renaming it" } else { "" }; - if let Some(sp) = tcx.hir().span_if_local(item.def_id) { + if let Some(sp) = tcx.hir_span_if_local(item.def_id) { err.span_label( sp, format!("`{}{}` defined here{}", prefix, item.name, rename_message), @@ -1381,7 +1381,7 @@ pub(crate) fn fn_trait_to_string( .find_map(|c| { if c.ident.name == sym::Output && let Some(ty) = c.ty() - && ty.span != tcx.hir().span(trait_segment.hir_id) + && ty.span != tcx.hir_span(trait_segment.hir_id) { tcx.sess.source_map().span_to_snippet(ty.span).ok() } else { diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs index 60a60f6415a22..21f0f9648ea30 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs @@ -92,7 +92,7 @@ fn generic_arg_mismatch_err( GenericArg::Type(hir::Ty { kind: hir::TyKind::Array(_, len), .. }), GenericParamDefKind::Const { .. }, ) if tcx.type_of(param.def_id).skip_binder() == tcx.types.usize => { - let snippet = sess.source_map().span_to_snippet(tcx.hir().span(len.hir_id)); + let snippet = sess.source_map().span_to_snippet(tcx.hir_span(len.hir_id)); if let Ok(snippet) = snippet { err.span_suggestion( arg.span(), diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 76a880da41855..76bc560025607 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -1066,7 +1066,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { let bound_span = tcx .associated_items(bound_id) .find_by_name_and_kind(tcx, assoc_name, assoc_kind, bound_id) - .and_then(|item| tcx.hir().span_if_local(item.def_id)); + .and_then(|item| tcx.hir_span_if_local(item.def_id)); if let Some(bound_span) = bound_span { err.span_label( @@ -1400,7 +1400,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { ); } - if let Some(sp) = tcx.hir().span_if_local(adt_def.did()) { + if let Some(sp) = tcx.hir_span_if_local(adt_def.did()) { err.span_label(sp, format!("variant `{assoc_ident}` not found here")); } diff --git a/compiler/rustc_hir_analysis/src/hir_wf_check.rs b/compiler/rustc_hir_analysis/src/hir_wf_check.rs index e27a81d4976f4..c01b81563dcf7 100644 --- a/compiler/rustc_hir_analysis/src/hir_wf_check.rs +++ b/compiler/rustc_hir_analysis/src/hir_wf_check.rs @@ -170,7 +170,7 @@ fn diagnostic_hir_wf_check<'tcx>( .. }) => vec![*ty], hir::Node::AnonConst(_) => { - if let Some(const_param_id) = tcx.hir().opt_const_param_default_param_def_id(hir_id) + if let Some(const_param_id) = tcx.hir_opt_const_param_default_param_def_id(hir_id) && let hir::Node::GenericParam(hir::GenericParam { kind: hir::GenericParamKind::Const { ty, .. }, .. diff --git a/compiler/rustc_hir_analysis/src/outlives/mod.rs b/compiler/rustc_hir_analysis/src/outlives/mod.rs index 32b05dcc569ef..daa908c8c78e4 100644 --- a/compiler/rustc_hir_analysis/src/outlives/mod.rs +++ b/compiler/rustc_hir_analysis/src/outlives/mod.rs @@ -25,7 +25,7 @@ fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[(ty::Clau } DefKind::AnonConst if tcx.features().generic_const_exprs() => { let id = tcx.local_def_id_to_hir_id(item_def_id); - if tcx.hir().opt_const_param_default_param_def_id(id).is_some() { + if tcx.hir_opt_const_param_default_param_def_id(id).is_some() { // In `generics_of` we set the generics' parent to be our parent's parent which means that // we lose out on the predicates of our actual parent if we dont return those predicates here. // (See comment in `generics_of` for more information on why the parent shenanigans is necessary) diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index 5e2daa69628f4..02fd7367e2f29 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -771,7 +771,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { format!("this {descr} returns an unsized value `{output_ty}`, so it cannot be called") ); if let DefIdOrName::DefId(def_id) = maybe_def - && let Some(def_span) = self.tcx.hir().span_if_local(def_id) + && let Some(def_span) = self.tcx.hir_span_if_local(def_id) { err.span_label(def_span, "the callable type is defined here"); } @@ -780,7 +780,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - if let Some(span) = self.tcx.hir().res_span(def) { + if let Some(span) = self.tcx.hir_res_span(def) { let callee_ty = callee_ty.to_string(); let label = match (unit_variant, inner_callee_path) { (Some((_, kind, path)), _) => { diff --git a/compiler/rustc_hir_typeck/src/closure.rs b/compiler/rustc_hir_typeck/src/closure.rs index a4776338f6c39..467ca26e7eab1 100644 --- a/compiler/rustc_hir_typeck/src/closure.rs +++ b/compiler/rustc_hir_typeck/src/closure.rs @@ -970,7 +970,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.typeck_results.borrow_mut().user_provided_sigs.insert(expr_def_id, c_result); // Normalize only after registering in `user_provided_sigs`. - self.normalize(self.tcx.hir().span(hir_id), result) + self.normalize(self.tcx.hir_span(hir_id), result) } /// Invoked when we are translating the coroutine that results diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index 41bdd0ca43ef1..b845e2190ef02 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -999,10 +999,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let container = with_no_trimmed_paths!(self.tcx.def_path_str(container_id)); for def_id in pick.import_ids { let hir_id = self.tcx.local_def_id_to_hir_id(def_id); - path_span.push_span_label( - self.tcx.hir().span(hir_id), - format!("`{container}` imported here"), - ); + path_span + .push_span_label(self.tcx.hir_span(hir_id), format!("`{container}` imported here")); } let tail = with_no_trimmed_paths!(match &other_methods_in_scope[..] { [] => return, diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index a75f6f4caac91..a882ea27af6bb 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -3239,7 +3239,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Some(x) => self.tcx.local_def_id_to_hir_id(x), None => return, }; - let param_span = self.tcx.hir().span(param_hir_id); + let param_span = self.tcx.hir_span(param_hir_id); let param_name = self.tcx.hir_ty_param_name(param_def_id.expect_local()); err.span_label(param_span, format!("type parameter '{param_name}' declared here")); diff --git a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs index 6fb289235de96..64176dacb73a7 100644 --- a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs @@ -1182,9 +1182,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx self.cx.error_reported_in_ty(ty)?; if ty.is_ty_var() { debug!("resolve_type_vars_or_bug: infer var from {:?}", ty); - Err(self - .cx - .report_bug(self.cx.tcx().hir().span(id), "encountered type variable")) + Err(self.cx.report_bug(self.cx.tcx().hir_span(id), "encountered type variable")) } else { Ok(ty) } @@ -1509,10 +1507,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx if node_ty != place_ty && self .cx - .try_structurally_resolve_type( - self.cx.tcx().hir().span(base_place.hir_id), - place_ty, - ) + .try_structurally_resolve_type(self.cx.tcx().hir_span(base_place.hir_id), place_ty) .is_impl_trait() { projections.push(Projection { kind: ProjectionKind::OpaqueCast, ty: node_ty }); @@ -1551,17 +1546,14 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx let base_curr_ty = base_place.place.ty(); let deref_ty = match self .cx - .try_structurally_resolve_type( - self.cx.tcx().hir().span(base_place.hir_id), - base_curr_ty, - ) + .try_structurally_resolve_type(self.cx.tcx().hir_span(base_place.hir_id), base_curr_ty) .builtin_deref(true) { Some(ty) => ty, None => { debug!("explicit deref of non-derefable type: {:?}", base_curr_ty); return Err(self.cx.report_bug( - self.cx.tcx().hir().span(node), + self.cx.tcx().hir_span(node), "explicit deref of non-derefable type", )); } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index d75c2853ba080..91190a32ff548 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -157,7 +157,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Lots of that diagnostics code relies on subtle effects of re-lowering, so we'll // let it keep doing that and just ensure that compilation won't succeed. self.dcx().span_delayed_bug( - self.tcx.hir().span(id), + self.tcx.hir_span(id), format!("`{prev}` overridden by `{ty}` for {id:?} in {:?}", self.body_id), ); } @@ -532,7 +532,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let ct = self.lowerer().lower_const_arg(const_arg, feed); self.register_wf_obligation( ct.into(), - self.tcx.hir().span(const_arg.hir_id), + self.tcx.hir_span(const_arg.hir_id), ObligationCauseCode::WellFormed(None), ); ct diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 264719ca56916..0097b6ea1231c 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -351,7 +351,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if self.suggest_fn_call(err, expr, found, |output| self.may_coerce(output, expected)) && let ty::FnDef(def_id, ..) = *found.kind() - && let Some(sp) = self.tcx.hir().span_if_local(def_id) + && let Some(sp) = self.tcx.hir_span_if_local(def_id) { let name = self.tcx.item_name(def_id); let kind = self.tcx.def_kind(def_id); @@ -407,7 +407,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { vec![(expr.span.shrink_to_hi(), format!(".{}()", conversion_method.name))] }; let struct_pat_shorthand_field = - self.tcx.hir().maybe_get_struct_pattern_shorthand_field(expr); + self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr); if let Some(name) = struct_pat_shorthand_field { sugg.insert(0, (expr.span.shrink_to_lo(), format!("{name}: "))); } @@ -449,7 +449,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }); let prefix_wrap = |sugg: &str| { - if let Some(name) = self.tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) { + if let Some(name) = self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) { format!(": {}{}", name, sugg) } else { sugg.to_string() @@ -671,7 +671,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } _ => { let prefix = if let Some(name) = - self.tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) + self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) { format!("{}: ", name) } else { @@ -1153,7 +1153,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }; if can_return && let Some(span) = expr.span.find_ancestor_inside( - self.tcx.hir().span_with_body(self.tcx.local_def_id_to_hir_id(fn_id)), + self.tcx.hir_span_with_body(self.tcx.local_def_id_to_hir_id(fn_id)), ) { // When the expr is in a match arm's body, we shouldn't add semicolon ';' at the end. @@ -1275,7 +1275,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) .must_apply_modulo_regions() { - let suggestion = match self.tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) { + let suggestion = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) { Some(ident) => format!(": {ident}.clone()"), None => ".clone()".to_string(), }; @@ -1381,7 +1381,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { (span.shrink_to_hi(), ").into()".to_owned()), ] }; - if let Some(name) = self.tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) { + if let Some(name) = self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) { sugg.insert(0, (expr.span.shrink_to_lo(), format!("{}: ", name))); } diag.multipart_suggestion( @@ -1436,7 +1436,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return false; } - let suggestion = match self.tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) { + let suggestion = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) { Some(ident) => format!(": {ident}.is_some()"), None => ".is_some()".to_string(), }; @@ -2032,7 +2032,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) }; - let sugg = match self.tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) { + let sugg = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) { Some(ident) => format!(": {ident}{sugg}"), None => sugg.to_string(), }; @@ -2289,7 +2289,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Unroll desugaring, to make sure this works for `for` loops etc. loop { parent = self.tcx.parent_hir_id(id); - let parent_span = self.tcx.hir().span(parent); + let parent_span = self.tcx.hir_span(parent); if parent_span.find_ancestor_inside(expr.span).is_some() { // The parent node is part of the same span, so is the result of the // same expansion/desugaring and not the 'real' parent node. @@ -2378,7 +2378,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .collect(); let suggestions_for = |variant: &_, ctor_kind, field_name| { - let prefix = match self.tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) { + let prefix = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) { Some(ident) => format!("{ident}: "), None => String::new(), }; @@ -2700,8 +2700,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { )); } - let prefix = match self.tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) - { + let prefix = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) { Some(ident) => format!("{ident}: "), None => String::new(), }; @@ -2911,7 +2910,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }; let prefix = - match self.tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) { + match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) { Some(ident) => format!("{ident}: "), None => String::new(), }; diff --git a/compiler/rustc_hir_typeck/src/intrinsicck.rs b/compiler/rustc_hir_typeck/src/intrinsicck.rs index 54e9e699353fa..194e420b606ea 100644 --- a/compiler/rustc_hir_typeck/src/intrinsicck.rs +++ b/compiler/rustc_hir_typeck/src/intrinsicck.rs @@ -45,7 +45,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { pub(crate) fn check_transmute(&self, from: Ty<'tcx>, to: Ty<'tcx>, hir_id: HirId) { let tcx = self.tcx; let dl = &tcx.data_layout; - let span = tcx.hir().span(hir_id); + let span = tcx.hir_span(hir_id); let normalize = |ty| { let ty = self.resolve_vars_if_possible(ty); if let Ok(ty) = diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index 4968998fd512b..46389668de7c3 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -117,7 +117,7 @@ fn typeck_with_inspect<'tcx>( let id = tcx.local_def_id_to_hir_id(def_id); let node = tcx.hir_node(id); - let span = tcx.hir().span(id); + let span = tcx.hir_span(id); // Figure out what primary body this item has. let body_id = node.body_id().unwrap_or_else(|| { diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 908c3ee2eb8e3..246b23f11b68b 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -254,11 +254,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { item_name ); err.span_label(item_name.span, format!("private {kind}")); - let sp = self - .tcx - .hir() - .span_if_local(def_id) - .unwrap_or_else(|| self.tcx.def_span(def_id)); + let sp = + self.tcx.hir_span_if_local(def_id).unwrap_or_else(|| self.tcx.def_span(def_id)); err.span_label(sp, format!("private {kind} defined here")); if let Some(within_macro_span) = within_macro_span { err.span_label(within_macro_span, "due to this macro variable"); @@ -566,7 +563,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { span.push_span_label(sugg_let.span, format!("`{rcvr_name}` of type `{self_ty}` that has method `{method_name}` defined earlier here")); span.push_span_label( - self.tcx.hir().span(recv_id), + self.tcx.hir_span(recv_id), format!( "earlier `{rcvr_name}` shadowed here with type `{ty_str_reported}`" ), @@ -2561,7 +2558,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ExprKind::Path(QPath::Resolved(_, path)) => { // local binding if let hir::def::Res::Local(hir_id) = path.res { - let span = tcx.hir().span(hir_id); + let span = tcx.hir_span(hir_id); let filename = tcx.sess.source_map().span_to_filename(span); let parent_node = self.tcx.parent_hir_node(hir_id); diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index f1f956779c94f..754d1e3144665 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -944,10 +944,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) { let var_ty = self.local_ty(span, var_id); if let Err(mut err) = self.demand_eqtype_pat_diag(span, var_ty, ty, ti) { - let hir = self.tcx.hir(); let var_ty = self.resolve_vars_if_possible(var_ty); let msg = format!("first introduced with type `{var_ty}` here"); - err.span_label(hir.span(var_id), msg); + err.span_label(self.tcx.hir_span(var_id), msg); let in_match = self.tcx.hir_parent_iter(var_id).any(|(_, n)| { matches!( n, @@ -1255,7 +1254,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { })) => match self.tcx.hir_node(body_id.hir_id) { hir::Node::Expr(expr) => { if hir::is_range_literal(expr) { - let span = self.tcx.hir().span(body_id.hir_id); + let span = self.tcx.hir_span(body_id.hir_id); if let Ok(snip) = self.tcx.sess.source_map().span_to_snippet(span) { e.span_suggestion_verbose( ident.span, @@ -1286,7 +1285,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { pat_ty: Ty<'tcx>, segments: &'tcx [hir::PathSegment<'tcx>], ) { - if let Some(span) = self.tcx.hir().res_span(pat_res) { + if let Some(span) = self.tcx.hir_res_span(pat_res) { e.span_label(span, format!("{} defined here", res.descr())); if let [hir::PathSegment { ident, .. }] = &*segments { e.span_label( diff --git a/compiler/rustc_hir_typeck/src/upvar.rs b/compiler/rustc_hir_typeck/src/upvar.rs index d07bfade15708..b4ac143f51353 100644 --- a/compiler/rustc_hir_typeck/src/upvar.rs +++ b/compiler/rustc_hir_typeck/src/upvar.rs @@ -635,7 +635,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let (place, capture_kind) = truncate_capture_for_optimization(place, capture_kind); let usage_span = if let Some(usage_expr) = capture_info.path_expr_id { - self.tcx.hir().span(usage_expr) + self.tcx.hir_span(usage_expr) } else { unreachable!() }; @@ -986,7 +986,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { for lint_note in diagnostics_info.iter() { match &lint_note.captures_info { UpvarMigrationInfo::CapturingPrecise { source_expr: Some(capture_expr_id), var_name: captured_name } => { - let cause_span = self.tcx.hir().span(*capture_expr_id); + let cause_span = self.tcx.hir_span(*capture_expr_id); lint.span_label(cause_span, format!("in Rust 2018, this closure captures all of `{}`, but in Rust 2021, it will only capture `{}`", self.tcx.hir_name(*var_hir_id), captured_name, @@ -1047,13 +1047,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { "add a dummy let to cause {migrated_variables_concat} to be fully captured" ); - let closure_span = self.tcx.hir().span_with_body(closure_hir_id); + let closure_span = self.tcx.hir_span_with_body(closure_hir_id); let mut closure_body_span = { // If the body was entirely expanded from a macro // invocation, i.e. the body is not contained inside the // closure span, then we walk up the expansion until we // find the span before the expansion. - let s = self.tcx.hir().span_with_body(body_id.hir_id); + let s = self.tcx.hir_span_with_body(body_id.hir_id); s.find_ancestor_inside(closure_span).unwrap_or(s) }; @@ -1752,8 +1752,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let capture_str = construct_capture_info_string(self.tcx, place, capture_info); let output_str = format!("Capturing {capture_str}"); - let span = - capture_info.path_expr_id.map_or(closure_span, |e| self.tcx.hir().span(e)); + let span = capture_info.path_expr_id.map_or(closure_span, |e| self.tcx.hir_span(e)); diag.span_note(span, output_str); } diag.emit(); @@ -1780,10 +1779,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if capture.info.path_expr_id != capture.info.capture_kind_expr_id { let path_span = capture_info .path_expr_id - .map_or(closure_span, |e| self.tcx.hir().span(e)); + .map_or(closure_span, |e| self.tcx.hir_span(e)); let capture_kind_span = capture_info .capture_kind_expr_id - .map_or(closure_span, |e| self.tcx.hir().span(e)); + .map_or(closure_span, |e| self.tcx.hir_span(e)); let mut multi_span: MultiSpan = MultiSpan::from_spans(vec![path_span, capture_kind_span]); @@ -1799,7 +1798,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } else { let span = capture_info .path_expr_id - .map_or(closure_span, |e| self.tcx.hir().span(e)); + .map_or(closure_span, |e| self.tcx.hir_span(e)); diag.span_note(span, output_str); }; @@ -1828,8 +1827,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let mut is_mutbl = bm.1; for pointer_ty in place.deref_tys() { - match self.structurally_resolve_type(self.tcx.hir().span(var_hir_id), pointer_ty).kind() - { + match self.structurally_resolve_type(self.tcx.hir_span(var_hir_id), pointer_ty).kind() { // We don't capture derefs of raw ptrs ty::RawPtr(_, _) => unreachable!(), @@ -1844,7 +1842,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ty::Adt(def, ..) if def.is_box() => {} unexpected_ty => span_bug!( - self.tcx.hir().span(var_hir_id), + self.tcx.hir_span(var_hir_id), "deref of unexpected pointer type {:?}", unexpected_ty ), @@ -1975,14 +1973,14 @@ fn drop_location_span(tcx: TyCtxt<'_>, hir_id: HirId) -> Span { let owner_node = tcx.hir_node(owner_id); let owner_span = match owner_node { hir::Node::Item(item) => match item.kind { - hir::ItemKind::Fn { body: owner_id, .. } => tcx.hir().span(owner_id.hir_id), + hir::ItemKind::Fn { body: owner_id, .. } => tcx.hir_span(owner_id.hir_id), _ => { bug!("Drop location span error: need to handle more ItemKind '{:?}'", item.kind); } }, - hir::Node::Block(block) => tcx.hir().span(block.hir_id), - hir::Node::TraitItem(item) => tcx.hir().span(item.hir_id()), - hir::Node::ImplItem(item) => tcx.hir().span(item.hir_id()), + hir::Node::Block(block) => tcx.hir_span(block.hir_id), + hir::Node::TraitItem(item) => tcx.hir_span(item.hir_id()), + hir::Node::ImplItem(item) => tcx.hir_span(item.hir_id()), _ => { bug!("Drop location span error: need to handle more Node '{:?}'", owner_node); } diff --git a/compiler/rustc_hir_typeck/src/writeback.rs b/compiler/rustc_hir_typeck/src/writeback.rs index b63c0b6ab7e09..d457133c703fd 100644 --- a/compiler/rustc_hir_typeck/src/writeback.rs +++ b/compiler/rustc_hir_typeck/src/writeback.rs @@ -491,7 +491,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { if let ty::UserTypeKind::TypeOf(_, user_args) = c_ty.value.kind { // This is a unit-testing mechanism. - let span = self.tcx().hir().span(hir_id); + let span = self.tcx().hir_span(hir_id); // We need to buffer the errors in order to guarantee a consistent // order when emitting them. let err = @@ -775,7 +775,7 @@ impl Locatable for Span { impl Locatable for HirId { fn to_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.hir().span(*self) + tcx.hir_span(*self) } } diff --git a/compiler/rustc_lint/src/impl_trait_overcaptures.rs b/compiler/rustc_lint/src/impl_trait_overcaptures.rs index c2404a7b84326..26481b97076f8 100644 --- a/compiler/rustc_lint/src/impl_trait_overcaptures.rs +++ b/compiler/rustc_lint/src/impl_trait_overcaptures.rs @@ -392,7 +392,7 @@ where } _ => { self.tcx.dcx().span_delayed_bug( - self.tcx.hir().span(arg.hir_id()), + self.tcx.hir_span(arg.hir_id()), "no valid for captured arg", ); } diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 8718fb807ecf5..313d8f6ba8ff4 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -450,6 +450,19 @@ impl<'s> LintLevelsBuilder<'s, TopDown> { builder } + pub fn crate_root( + sess: &'s Session, + features: &'s Features, + lint_added_lints: bool, + store: &'s LintStore, + registered_tools: &'s RegisteredTools, + crate_attrs: &[ast::Attribute], + ) -> Self { + let mut builder = Self::new(sess, features, lint_added_lints, store, registered_tools); + builder.add(crate_attrs, true, None); + builder + } + fn process_command_line(&mut self) { self.provider.cur = self .provider diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index 25878c7ac8144..9b5c564d3324b 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -130,6 +130,7 @@ pub use context::{ }; pub use early::{EarlyCheckNode, check_ast_node}; pub use late::{check_crate, late_lint_mod, unerased_lint_store}; +pub use levels::LintLevelsBuilder; pub use passes::{EarlyLintPass, LateLintPass}; pub use rustc_session::lint::Level::{self, *}; pub use rustc_session::lint::{ diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 6966985bdf059..fec5db64bf28e 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -1403,7 +1403,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { CItemKind::Definition => "fn", }; let span_note = if let ty::Adt(def, _) = ty.kind() - && let Some(sp) = self.cx.tcx.hir().span_if_local(def.did()) + && let Some(sp) = self.cx.tcx.hir_span_if_local(def.did()) { Some(sp) } else { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 386d3d3156a1c..1e9f342b6c76b 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1882,8 +1882,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let is_proc_macro = self.tcx.crate_types().contains(&CrateType::ProcMacro); if is_proc_macro { let tcx = self.tcx; - let hir = tcx.hir(); - let proc_macro_decls_static = tcx.proc_macro_decls_static(()).unwrap().local_def_index; let stability = tcx.lookup_stability(CRATE_DEF_ID); let macros = @@ -1916,7 +1914,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let id = proc_macro; let proc_macro = tcx.local_def_id_to_hir_id(proc_macro); let mut name = tcx.hir_name(proc_macro); - let span = hir.span(proc_macro); + let span = tcx.hir_span(proc_macro); // Proc-macros may have attributes like `#[allow_internal_unstable]`, // so downstream crates need access to them. let attrs = tcx.hir_attrs(proc_macro); diff --git a/compiler/rustc_middle/src/hir/map.rs b/compiler/rustc_middle/src/hir/map.rs index 52f155a16b868..12618e798238f 100644 --- a/compiler/rustc_middle/src/hir/map.rs +++ b/compiler/rustc_middle/src/hir/map.rs @@ -22,6 +22,7 @@ use crate::ty::TyCtxt; // only serves as "namespace" for HIR-related methods, and can be // removed if all the methods are reasonably renamed and moved to tcx // (https://github.com/rust-lang/rust/pull/118256#issuecomment-1826442834). +#[allow(unused)] // FIXME: temporary #[derive(Copy, Clone)] pub struct Map<'hir> { pub(super) tcx: TyCtxt<'hir>, @@ -273,7 +274,7 @@ impl<'tcx> TyCtxt<'tcx> { self.hir_maybe_body_owned_by(id).unwrap_or_else(|| { let hir_id = self.local_def_id_to_hir_id(id); span_bug!( - self.hir().span(hir_id), + self.hir_span(hir_id), "body_owned_by: {} has no associated body", self.hir_id_to_string(hir_id) ); @@ -681,9 +682,8 @@ impl<'tcx> TyCtxt<'tcx> { pub fn hir_id_to_string(self, id: HirId) -> String { let path_str = |def_id: LocalDefId| self.def_path_str(def_id); - let span_str = || { - self.sess.source_map().span_to_snippet(Map { tcx: self }.span(id)).unwrap_or_default() - }; + let span_str = + || self.sess.source_map().span_to_snippet(self.hir_span(id)).unwrap_or_default(); let node_str = |prefix| format!("{id} ({prefix} `{}`)", span_str()); match self.hir_node(id) { @@ -879,12 +879,10 @@ impl<'tcx> TyCtxt<'tcx> { pub fn hir_attrs(self, id: HirId) -> &'tcx [Attribute] { self.hir_attr_map(id.owner).get(id.local_id) } -} -impl<'hir> Map<'hir> { /// Gets the span of the definition of the specified HIR node. /// This is used by `tcx.def_span`. - pub fn span(self, hir_id: HirId) -> Span { + pub fn hir_span(self, hir_id: HirId) -> Span { fn until_within(outer: Span, end: Span) -> Span { if let Some(end) = end.find_ancestor_inside(outer) { outer.with_hi(end.hi()) @@ -904,7 +902,7 @@ impl<'hir> Map<'hir> { span } - let span = match self.tcx.hir_node(hir_id) { + let span = match self.hir_node(hir_id) { // Function-like. Node::Item(Item { kind: ItemKind::Fn { sig, .. }, span: outer_span, .. }) | Node::TraitItem(TraitItem { @@ -984,7 +982,7 @@ impl<'hir> Map<'hir> { Node::Variant(variant) => named_span(variant.span, variant.ident, None), Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)), Node::ForeignItem(item) => named_span(item.span, item.ident, None), - Node::Ctor(_) => return self.span(self.tcx.parent_hir_id(hir_id)), + Node::Ctor(_) => return self.hir_span(self.parent_hir_id(hir_id)), Node::Expr(Expr { kind: ExprKind::Closure(Closure { fn_decl_span, .. }), span, @@ -993,16 +991,16 @@ impl<'hir> Map<'hir> { // Ensure that the returned span has the item's SyntaxContext. fn_decl_span.find_ancestor_inside(*span).unwrap_or(*span) } - _ => self.span_with_body(hir_id), + _ => self.hir_span_with_body(hir_id), }; - debug_assert_eq!(span.ctxt(), self.span_with_body(hir_id).ctxt()); + debug_assert_eq!(span.ctxt(), self.hir_span_with_body(hir_id).ctxt()); span } - /// Like `hir.span()`, but includes the body of items + /// Like `hir_span()`, but includes the body of items /// (instead of just the item header) - pub fn span_with_body(self, hir_id: HirId) -> Span { - match self.tcx.hir_node(hir_id) { + pub fn hir_span_with_body(self, hir_id: HirId) -> Span { + match self.hir_node(hir_id) { Node::Param(param) => param.span, Node::Item(item) => item.span, Node::ForeignItem(foreign_item) => foreign_item.span, @@ -1011,7 +1009,7 @@ impl<'hir> Map<'hir> { Node::Variant(variant) => variant.span, Node::Field(field) => field.span, Node::AnonConst(constant) => constant.span, - Node::ConstBlock(constant) => self.tcx.hir_body(constant.body).value.span, + Node::ConstBlock(constant) => self.hir_body(constant.body).value.span, Node::ConstArg(const_arg) => const_arg.span(), Node::Expr(expr) => expr.span, Node::ExprField(field) => field.span, @@ -1031,7 +1029,7 @@ impl<'hir> Map<'hir> { Node::PatExpr(lit) => lit.span, Node::Arm(arm) => arm.span, Node::Block(block) => block.span, - Node::Ctor(..) => self.span_with_body(self.tcx.parent_hir_id(hir_id)), + Node::Ctor(..) => self.hir_span_with_body(self.parent_hir_id(hir_id)), Node::Lifetime(lifetime) => lifetime.ident.span, Node::GenericParam(param) => param.span, Node::Infer(i) => i.span, @@ -1044,23 +1042,23 @@ impl<'hir> Map<'hir> { } } - pub fn span_if_local(self, id: DefId) -> Option { - id.is_local().then(|| self.tcx.def_span(id)) + pub fn hir_span_if_local(self, id: DefId) -> Option { + id.is_local().then(|| self.def_span(id)) } - pub fn res_span(self, res: Res) -> Option { + pub fn hir_res_span(self, res: Res) -> Option { match res { Res::Err => None, - Res::Local(id) => Some(self.span(id)), - res => self.span_if_local(res.opt_def_id()?), + Res::Local(id) => Some(self.hir_span(id)), + res => self.hir_span_if_local(res.opt_def_id()?), } } /// Returns the HirId of `N` in `struct Foo` when /// called with the HirId for the `{ ... }` anon const - pub fn opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option { - let const_arg = self.tcx.parent_hir_id(anon_const); - match self.tcx.parent_hir_node(const_arg) { + pub fn hir_opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option { + let const_arg = self.parent_hir_id(anon_const); + match self.parent_hir_node(const_arg) { Node::GenericParam(GenericParam { def_id: param_id, kind: GenericParamKind::Const { .. }, @@ -1070,7 +1068,7 @@ impl<'hir> Map<'hir> { } } - pub fn maybe_get_struct_pattern_shorthand_field(&self, expr: &Expr<'_>) -> Option { + pub fn hir_maybe_get_struct_pattern_shorthand_field(self, expr: &Expr<'_>) -> Option { let local = match expr { Expr { kind: @@ -1085,7 +1083,7 @@ impl<'hir> Map<'hir> { _ => None, }?; - match self.tcx.parent_hir_node(expr.hir_id) { + match self.parent_hir_node(expr.hir_id) { Node::ExprField(field) => { if field.ident.name == local.name && field.is_shorthand { return Some(local.name); diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 347bc5ea31289..03fca8aff968f 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -210,13 +210,12 @@ pub fn provide(providers: &mut Providers) { providers.hir_attr_map = |tcx, id| { tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs) }; - providers.def_span = |tcx, def_id| tcx.hir().span(tcx.local_def_id_to_hir_id(def_id)); + providers.def_span = |tcx, def_id| tcx.hir_span(tcx.local_def_id_to_hir_id(def_id)); providers.def_ident_span = |tcx, def_id| { let hir_id = tcx.local_def_id_to_hir_id(def_id); tcx.hir_opt_ident_span(hir_id) }; providers.fn_arg_names = |tcx, def_id| { - let hir = tcx.hir(); if let Some(body_id) = tcx.hir_node_by_def_id(def_id).body_id() { tcx.arena.alloc_from_iter(tcx.hir_body_param_names(body_id)) } else if let Node::TraitItem(&TraitItem { @@ -231,7 +230,7 @@ pub fn provide(providers: &mut Providers) { idents } else { span_bug!( - hir.span(tcx.local_def_id_to_hir_id(def_id)), + tcx.hir_span(tcx.local_def_id_to_hir_id(def_id)), "fn_arg_names: unexpected item {:?}", def_id ); diff --git a/compiler/rustc_middle/src/middle/region.rs b/compiler/rustc_middle/src/middle/region.rs index ba31f775b651e..92eab59dd0274 100644 --- a/compiler/rustc_middle/src/middle/region.rs +++ b/compiler/rustc_middle/src/middle/region.rs @@ -175,7 +175,7 @@ impl Scope { let Some(hir_id) = self.hir_id(scope_tree) else { return DUMMY_SP; }; - let span = tcx.hir().span(hir_id); + let span = tcx.hir_span(hir_id); if let ScopeData::Remainder(first_statement_index) = self.data { if let Node::Block(blk) = tcx.hir_node(hir_id) { // Want span for scope starting after the diff --git a/compiler/rustc_middle/src/query/keys.rs b/compiler/rustc_middle/src/query/keys.rs index c382bcd726ffa..9ed1f10455ad3 100644 --- a/compiler/rustc_middle/src/query/keys.rs +++ b/compiler/rustc_middle/src/query/keys.rs @@ -586,7 +586,7 @@ impl Key for HirId { type Cache = DefaultCache; fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.hir().span(*self) + tcx.hir_span(*self) } #[inline(always)] @@ -599,7 +599,7 @@ impl Key for (LocalDefId, HirId) { type Cache = DefaultCache; fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.hir().span(self.1) + tcx.hir_span(self.1) } #[inline(always)] diff --git a/compiler/rustc_middle/src/ty/closure.rs b/compiler/rustc_middle/src/ty/closure.rs index 703b6ce92471f..ff9096695d4db 100644 --- a/compiler/rustc_middle/src/ty/closure.rs +++ b/compiler/rustc_middle/src/ty/closure.rs @@ -150,9 +150,9 @@ impl<'tcx> CapturedPlace<'tcx> { /// Return span pointing to use that resulted in selecting the captured path pub fn get_path_span(&self, tcx: TyCtxt<'tcx>) -> Span { if let Some(path_expr_id) = self.info.path_expr_id { - tcx.hir().span(path_expr_id) + tcx.hir_span(path_expr_id) } else if let Some(capture_kind_expr_id) = self.info.capture_kind_expr_id { - tcx.hir().span(capture_kind_expr_id) + tcx.hir_span(capture_kind_expr_id) } else { // Fallback on upvars mentioned if neither path or capture expr id is captured @@ -166,9 +166,9 @@ impl<'tcx> CapturedPlace<'tcx> { /// Return span pointing to use that resulted in selecting the current capture kind pub fn get_capture_kind_span(&self, tcx: TyCtxt<'tcx>) -> Span { if let Some(capture_kind_expr_id) = self.info.capture_kind_expr_id { - tcx.hir().span(capture_kind_expr_id) + tcx.hir_span(capture_kind_expr_id) } else if let Some(path_expr_id) = self.info.path_expr_id { - tcx.hir().span(path_expr_id) + tcx.hir_span(path_expr_id) } else { // Fallback on upvars mentioned if neither path or capture expr id is captured diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index e3b3eccffb550..55ebd15248c61 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -720,7 +720,7 @@ impl<'tcx> Instance<'tcx> { ty::TypingEnv::fully_monomorphized(), def_id, args, - ty.ty_adt_def().and_then(|adt| tcx.hir().span_if_local(adt.did())).unwrap_or(DUMMY_SP), + ty.ty_adt_def().and_then(|adt| tcx.hir_span_if_local(adt.did())).unwrap_or(DUMMY_SP), ) } @@ -732,7 +732,7 @@ impl<'tcx> Instance<'tcx> { ty::TypingEnv::fully_monomorphized(), def_id, args, - ty.ty_adt_def().and_then(|adt| tcx.hir().span_if_local(adt.did())).unwrap_or(DUMMY_SP), + ty.ty_adt_def().and_then(|adt| tcx.hir_span_if_local(adt.did())).unwrap_or(DUMMY_SP), ) } diff --git a/compiler/rustc_mir_build/src/builder/expr/as_place.rs b/compiler/rustc_mir_build/src/builder/expr/as_place.rs index 50ca924baf9c8..90e27c85f749a 100644 --- a/compiler/rustc_mir_build/src/builder/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/builder/expr/as_place.rs @@ -258,7 +258,7 @@ impl<'tcx> PlaceBuilder<'tcx> { self.projection ), PlaceBase::Upvar { var_hir_id, closure_def_id: _ } => span_bug!( - cx.tcx.hir().span(var_hir_id.0), + cx.tcx.hir_span(var_hir_id.0), "could not resolve upvar: {var_hir_id:?} + {:?}", self.projection ), diff --git a/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs index 97d34b85f50da..f9791776f71e5 100644 --- a/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs @@ -416,7 +416,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { if let Some(mir_place) = place_builder.try_to_place(this) { this.cfg.push_fake_read( block, - this.source_info(this.tcx.hir().span(*hir_id)), + this.source_info(this.tcx.hir_span(*hir_id)), *cause, mir_place, ); diff --git a/compiler/rustc_mir_build/src/builder/mod.rs b/compiler/rustc_mir_build/src/builder/mod.rs index c8b69a6ec62f5..7396591e1853e 100644 --- a/compiler/rustc_mir_build/src/builder/mod.rs +++ b/compiler/rustc_mir_build/src/builder/mod.rs @@ -457,7 +457,7 @@ fn construct_fn<'tcx>( // Figure out what primary body this item has. let body = tcx.hir_body_owned_by(fn_def); - let span_with_body = tcx.hir().span_with_body(fn_id); + let span_with_body = tcx.hir_span_with_body(fn_id); let return_ty_span = tcx .hir_fn_decl_by_hir_id(fn_id) .unwrap_or_else(|| span_bug!(span, "can't build MIR for {:?}", fn_def)) diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 7f2e7d5ca8385..2a9bfb25b8421 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -943,7 +943,7 @@ impl UnsafeOpKind { } }); let unsafe_not_inherited_note = if let Some((id, _)) = note_non_inherited { - let span = tcx.hir().span(id); + let span = tcx.hir_span(id); let span = tcx.sess.source_map().guess_head_span(span); Some(UnsafeNotInheritedNote { span }) } else { diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 095d3e75da1ee..ea8c7303c0afa 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -1174,7 +1174,7 @@ fn report_arm_reachability<'p, 'tcx>( for (arm, is_useful) in report.arm_usefulness.iter() { if let Usefulness::Redundant(explanation) = is_useful { let hir_id = arm.arm_data; - let arm_span = cx.tcx.hir().span(hir_id); + let arm_span = cx.tcx.hir_span(hir_id); let whole_arm_span = if is_match_arm { // If the arm is followed by a comma, extend the span to include it. let with_whitespace = sm.span_extend_while_whitespace(arm_span); diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index cfc0369c5989b..669349f3380aa 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -1157,7 +1157,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { errors::DocInlineOnlyUse { attr_span: meta.span(), item_span: (attr.style() == AttrStyle::Outer) - .then(|| self.tcx.hir().span(hir_id)), + .then(|| self.tcx.hir_span(hir_id)), }, ); } @@ -1179,7 +1179,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { errors::DocMaskedOnlyExternCrate { attr_span: meta.span(), item_span: (attr.style() == AttrStyle::Outer) - .then(|| self.tcx.hir().span(hir_id)), + .then(|| self.tcx.hir_span(hir_id)), }, ); return; @@ -1193,7 +1193,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { errors::DocMaskedNotExternCrateSelf { attr_span: meta.span(), item_span: (attr.style() == AttrStyle::Outer) - .then(|| self.tcx.hir().span(hir_id)), + .then(|| self.tcx.hir_span(hir_id)), }, ); } diff --git a/compiler/rustc_passes/src/diagnostic_items.rs b/compiler/rustc_passes/src/diagnostic_items.rs index e13d94c1031ac..17a729f422a1c 100644 --- a/compiler/rustc_passes/src/diagnostic_items.rs +++ b/compiler/rustc_passes/src/diagnostic_items.rs @@ -41,8 +41,8 @@ fn report_duplicate_item( original_def_id: DefId, item_def_id: DefId, ) { - let orig_span = tcx.hir().span_if_local(original_def_id); - let duplicate_span = tcx.hir().span_if_local(item_def_id); + let orig_span = tcx.hir_span_if_local(original_def_id); + let duplicate_span = tcx.hir_span_if_local(item_def_id); tcx.dcx().emit_err(DuplicateDiagnosticItemInCrate { duplicate_span, orig_span, diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index 9e55914f8f2f7..6dec96f9f502f 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -726,7 +726,7 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index { annotator.annotate( CRATE_DEF_ID, - tcx.hir().span(CRATE_HIR_ID), + tcx.hir_span(CRATE_HIR_ID), None, AnnotationKind::Required, InheritDeprecation::Yes, @@ -1099,7 +1099,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) { if is_staged_api { let effective_visibilities = &tcx.effective_visibilities(()); let mut missing = MissingStabilityAnnotations { tcx, effective_visibilities }; - missing.check_missing_stability(CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID)); + missing.check_missing_stability(CRATE_DEF_ID, tcx.hir_span(CRATE_HIR_ID)); tcx.hir_walk_toplevel_module(&mut missing); tcx.hir_visit_all_item_likes_in_crate(&mut missing); } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 612e091770f0a..3ac66840d87de 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -88,6 +88,8 @@ mod late; mod macros; pub mod rustdoc; +pub use macros::registered_tools_ast; + rustc_fluent_macro::fluent_messages! { "../messages.ftl" } #[derive(Debug)] diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index d0ffef798cf6c..9d6ae0aa9d130 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -10,7 +10,7 @@ use rustc_ast::{self as ast, Crate, NodeId, attr}; use rustc_ast_pretty::pprust; use rustc_attr_parsing::{AttributeKind, StabilityLevel, find_attr}; use rustc_data_structures::intern::Interned; -use rustc_errors::{Applicability, StashKey}; +use rustc_errors::{Applicability, DiagCtxtHandle, StashKey}; use rustc_expand::base::{ DeriveResolution, Indeterminate, ResolverExpand, SyntaxExtension, SyntaxExtensionKind, }; @@ -124,14 +124,21 @@ fn fast_print_path(path: &ast::Path) -> Symbol { } pub(crate) fn registered_tools(tcx: TyCtxt<'_>, (): ()) -> RegisteredTools { - let mut registered_tools = RegisteredTools::default(); let (_, pre_configured_attrs) = &*tcx.crate_for_resolver(()).borrow(); + registered_tools_ast(tcx.dcx(), pre_configured_attrs) +} + +pub fn registered_tools_ast( + dcx: DiagCtxtHandle<'_>, + pre_configured_attrs: &[ast::Attribute], +) -> RegisteredTools { + let mut registered_tools = RegisteredTools::default(); for attr in attr::filter_by_name(pre_configured_attrs, sym::register_tool) { for meta_item_inner in attr.meta_item_list().unwrap_or_default() { match meta_item_inner.ident() { Some(ident) => { if let Some(old_ident) = registered_tools.replace(ident) { - tcx.dcx().emit_err(errors::ToolWasAlreadyRegistered { + dcx.emit_err(errors::ToolWasAlreadyRegistered { span: ident.span, tool: ident, old_ident_span: old_ident.span, @@ -139,7 +146,7 @@ pub(crate) fn registered_tools(tcx: TyCtxt<'_>, (): ()) -> RegisteredTools { } } None => { - tcx.dcx().emit_err(errors::ToolOnlyAcceptsIdentifiers { + dcx.emit_err(errors::ToolOnlyAcceptsIdentifiers { span: meta_item_inner.span(), tool: sym::register_tool, }); diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index ed336cc559612..1b01efda2a9ea 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -50,6 +50,7 @@ pub const PRINT_KINDS: &[(&str, PrintKind)] = &[ ("check-cfg", PrintKind::CheckCfg), ("code-models", PrintKind::CodeModels), ("crate-name", PrintKind::CrateName), + ("crate-root-lint-levels", PrintKind::CrateRootLintLevels), ("deployment-target", PrintKind::DeploymentTarget), ("file-names", PrintKind::FileNames), ("host-tuple", PrintKind::HostTuple), @@ -881,6 +882,7 @@ pub enum PrintKind { CheckCfg, CodeModels, CrateName, + CrateRootLintLevels, DeploymentTarget, FileNames, HostTuple, @@ -2067,6 +2069,7 @@ fn check_print_request_stability( match print_kind { PrintKind::AllTargetSpecsJson | PrintKind::CheckCfg + | PrintKind::CrateRootLintLevels | PrintKind::SupportedCrateTypes | PrintKind::TargetSpecJson if !unstable_opts.unstable_options => diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs index 0bcb5f6f3b286..f45e3904212c4 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs @@ -1051,7 +1051,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { None? } let args = self.node_args_opt(expr.hir_id)?; - let span = tcx.hir().span(segment.hir_id); + let span = tcx.hir_span(segment.hir_id); let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi()); InsertableGenericArgs { insert_span, @@ -1110,7 +1110,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { if generics.has_impl_trait() { return None; } - let span = tcx.hir().span(segment.hir_id); + let span = tcx.hir_span(segment.hir_id); let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi()); Some(InsertableGenericArgs { insert_span, @@ -1144,7 +1144,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { if !segment.infer_args || generics.has_impl_trait() { do yeet (); } - let span = tcx.hir().span(segment.hir_id); + let span = tcx.hir_span(segment.hir_id); let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi()); InsertableGenericArgs { insert_span, diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/util.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/util.rs index 00f053fa599e0..683b5b528c6eb 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/util.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/util.rs @@ -51,7 +51,6 @@ pub fn find_param_with_region<'tcx>( _ => return None, // not a free region }; - let hir = &tcx.hir(); let def_id = id.as_local()?; // FIXME: use def_kind @@ -93,7 +92,7 @@ pub fn find_param_with_region<'tcx>( }); found_anon_region.then(|| { let ty_hir_id = fn_decl.inputs[index].hir_id; - let param_ty_span = hir.span(ty_hir_id); + let param_ty_span = tcx.hir_span(ty_hir_id); let is_first = index == 0; AnonymousParamInfo { param, param_ty: new_param_ty, param_ty_span, kind, is_first } }) diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs index 07a67cde3be1c..73ae5177c4895 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs @@ -1236,7 +1236,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { ); // Only suggest derive if this isn't a derived obligation, // and the struct is local. - if let Some(span) = self.tcx.hir().span_if_local(def.did()) + if let Some(span) = self.tcx.hir_span_if_local(def.did()) && obligation.cause.code().parent().is_none() { if ty.is_structural_eq_shallow(self.tcx) { @@ -2943,7 +2943,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { }; let found_node = found_did.and_then(|did| self.tcx.hir_get_if_local(did)); - let found_span = found_did.and_then(|did| self.tcx.hir().span_if_local(did)); + let found_span = found_did.and_then(|did| self.tcx.hir_span_if_local(did)); if !self.reported_signature_mismatch.borrow_mut().insert((span, found_span)) { // We check closures twice, with obligations flowing in different directions, @@ -3030,7 +3030,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { node: Node<'_>, ) -> Option<(Span, Option, Vec)> { let sm = self.tcx.sess.source_map(); - let hir = self.tcx.hir(); Some(match node { Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, fn_arg_span, .. }), @@ -3086,7 +3085,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { .collect::>(), ), Node::Ctor(variant_data) => { - let span = variant_data.ctor_hir_id().map_or(DUMMY_SP, |id| hir.span(id)); + let span = variant_data.ctor_hir_id().map_or(DUMMY_SP, |id| self.tcx.hir_span(id)); (span, None, vec![ArgKind::empty(); variant_data.fields().len()]) } _ => panic!("non-FnLike node found: {node:?}"), diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs index 98df09b6f7b01..efee6e2aa1d45 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs @@ -398,7 +398,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { ); if !self.tcx.is_impl_trait_in_trait(trait_item_def_id) { - if let Some(span) = self.tcx.hir().span_if_local(trait_item_def_id) { + if let Some(span) = self.tcx.hir_span_if_local(trait_item_def_id) { let item_name = self.tcx.item_name(impl_item_def_id.to_def_id()); err.span_label(span, format!("definition of `{item_name}` from trait")); } diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index dc8022b95c313..38fcba4ea6256 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -1193,7 +1193,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { // FIXME(compiler-errors): This is kind of a mess, but required for obligations // that come from a path expr to affect the *call* expr. c @ ObligationCauseCode::WhereClauseInExpr(_, _, hir_id, _) - if self.tcx.hir().span(*hir_id).lo() == span.lo() => + if self.tcx.hir_span(*hir_id).lo() == span.lo() => { c } @@ -4481,7 +4481,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { Obligation::new(self.tcx, obligation.cause.clone(), obligation.param_env, trait_ref); if self.predicate_must_hold_modulo_regions(&obligation) { - let arg_span = self.tcx.hir().span(*arg_hir_id); + let arg_span = self.tcx.hir_span(*arg_hir_id); err.multipart_suggestion_verbose( format!("use a unary tuple instead"), vec![(arg_span.shrink_to_lo(), "(".into()), (arg_span.shrink_to_hi(), ",)".into())], @@ -4521,7 +4521,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { parent_code: _, } = cause.code() { - let arg_span = self.tcx.hir().span(*arg_hir_id); + let arg_span = self.tcx.hir_span(*arg_hir_id); let mut sp: MultiSpan = arg_span.into(); sp.push_span_label( @@ -4530,7 +4530,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { generic types that should be inferred from this argument", ); sp.push_span_label( - self.tcx.hir().span(*call_hir_id), + self.tcx.hir_span(*call_hir_id), "add turbofish arguments to this call to \ specify the types manually, even if it's redundant", ); @@ -4939,7 +4939,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { .type_implements_trait(pred.def_id(), [rhs_ty, lhs_ty], param_env) .must_apply_modulo_regions() { - let lhs_span = tcx.hir().span(lhs_hir_id); + let lhs_span = tcx.hir_span(lhs_hir_id); let sm = tcx.sess.source_map(); if let Ok(rhs_snippet) = sm.span_to_snippet(rhs_span) && let Ok(lhs_snippet) = sm.span_to_snippet(lhs_span) diff --git a/src/doc/unstable-book/src/compiler-flags/print-crate-root-lint-levels.md b/src/doc/unstable-book/src/compiler-flags/print-crate-root-lint-levels.md new file mode 100644 index 0000000000000..0bad8b8ab96bd --- /dev/null +++ b/src/doc/unstable-book/src/compiler-flags/print-crate-root-lint-levels.md @@ -0,0 +1,23 @@ +# `print=crate-root-lint-levels` + +The tracking issue for this feature is: [#139180](https://github.com/rust-lang/rust/issues/139180). + +------------------------ + +This option of the `--print` flag print the list of lints with print out all the lints and their associated levels (`allow`, `warn`, `deny`, `forbid`) based on the regular Rust rules at crate root, that is *(roughly)*: + - command line args (`-W`, `-A`, `--force-warn`, `--cap-lints`, ...) + - crate root attributes (`#![allow]`, `#![warn]`, `#[expect]`, ...) + - *the special `warnings` lint group* + - the default lint level + +The output format is `LINT_NAME=LINT_LEVEL`, e.g.: +```text +unknown_lint=warn +arithmetic_overflow=deny +``` + +To be used like this: + +```bash +rustc --print=crate-root-lint-levels -Zunstable-options lib.rs +``` diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 143191a6f2a0e..d3ddb77c0b33c 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -367,10 +367,7 @@ impl fmt::Debug for Item { pub(crate) fn rustc_span(def_id: DefId, tcx: TyCtxt<'_>) -> Span { Span::new(def_id.as_local().map_or_else( || tcx.def_span(def_id), - |local| { - let hir = tcx.hir(); - hir.span_with_body(tcx.local_def_id_to_hir_id(local)) - }, + |local| tcx.hir_span_with_body(tcx.local_def_id_to_hir_id(local)), )) } diff --git a/src/librustdoc/doctest/rust.rs b/src/librustdoc/doctest/rust.rs index 12f8b26b1e3c6..be2dea641d79e 100644 --- a/src/librustdoc/doctest/rust.rs +++ b/src/librustdoc/doctest/rust.rs @@ -80,7 +80,7 @@ impl<'tcx> HirCollector<'tcx> { pub fn collect_crate(mut self) -> Vec { let tcx = self.tcx; - self.visit_testable(None, CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID), |this| { + self.visit_testable(None, CRATE_DEF_ID, tcx.hir_span(CRATE_HIR_ID), |this| { tcx.hir_walk_toplevel_module(this) }); self.collector.tests diff --git a/src/librustdoc/html/render/span_map.rs b/src/librustdoc/html/render/span_map.rs index 3676051b1bd29..846d3ad310ce4 100644 --- a/src/librustdoc/html/render/span_map.rs +++ b/src/librustdoc/html/render/span_map.rs @@ -95,7 +95,7 @@ impl SpanMapVisitor<'_> { .unwrap_or(path.span); self.matches.insert(span, link); } - Res::Local(_) if let Some(span) = self.tcx.hir().res_span(path.res) => { + Res::Local(_) if let Some(span) = self.tcx.hir_res_span(path.res) => { self.matches.insert(path.span, LinkFromSrc::Local(clean::Span::new(span))); } Res::PrimTy(p) => { diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index 44551d75903da..fceacb69fb55e 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -176,8 +176,7 @@ where // If the enclosing item has a span coming from a proc macro, then we also don't want to // include the example. - let enclosing_item_span = - tcx.hir().span_with_body(tcx.hir_get_parent_item(ex.hir_id).into()); + let enclosing_item_span = tcx.hir_span_with_body(tcx.hir_get_parent_item(ex.hir_id).into()); if enclosing_item_span.from_expansion() { trace!("Rejecting expr ({call_span:?}) from macro item: {enclosing_item_span:?}"); return; diff --git a/src/tools/clippy/clippy_lints/src/casts/unnecessary_cast.rs b/src/tools/clippy/clippy_lints/src/casts/unnecessary_cast.rs index 7885f171461d7..72953818a708e 100644 --- a/src/tools/clippy/clippy_lints/src/casts/unnecessary_cast.rs +++ b/src/tools/clippy/clippy_lints/src/casts/unnecessary_cast.rs @@ -143,7 +143,7 @@ pub(super) fn check<'tcx>( if cast_from.kind() == cast_to.kind() && !expr.span.in_external_macro(cx.sess().source_map()) { if let Some(id) = path_to_local(cast_expr) - && !cx.tcx.hir().span(id).eq_ctxt(cast_expr.span) + && !cx.tcx.hir_span(id).eq_ctxt(cast_expr.span) { // Binding context is different than the identifiers context. // Weird macro wizardry could be involved here. diff --git a/src/tools/clippy/clippy_lints/src/derive.rs b/src/tools/clippy/clippy_lints/src/derive.rs index 2ae35b4005579..7480b409100c0 100644 --- a/src/tools/clippy/clippy_lints/src/derive.rs +++ b/src/tools/clippy/clippy_lints/src/derive.rs @@ -254,7 +254,7 @@ fn check_hash_peq<'tcx>( |diag| { if let Some(local_def_id) = impl_id.as_local() { let hir_id = cx.tcx.local_def_id_to_hir_id(local_def_id); - diag.span_note(cx.tcx.hir().span(hir_id), "`PartialEq` implemented here"); + diag.span_note(cx.tcx.hir_span(hir_id), "`PartialEq` implemented here"); } }, ); @@ -298,7 +298,7 @@ fn check_ord_partial_ord<'tcx>( span_lint_and_then(cx, DERIVE_ORD_XOR_PARTIAL_ORD, span, mess, |diag| { if let Some(local_def_id) = impl_id.as_local() { let hir_id = cx.tcx.local_def_id_to_hir_id(local_def_id); - diag.span_note(cx.tcx.hir().span(hir_id), "`PartialOrd` implemented here"); + diag.span_note(cx.tcx.hir_span(hir_id), "`PartialOrd` implemented here"); } }); } diff --git a/src/tools/clippy/clippy_lints/src/escape.rs b/src/tools/clippy/clippy_lints/src/escape.rs index de0fc2b1bf4bb..e248f08789b2d 100644 --- a/src/tools/clippy/clippy_lints/src/escape.rs +++ b/src/tools/clippy/clippy_lints/src/escape.rs @@ -120,7 +120,7 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal { cx, BOXED_LOCAL, node, - cx.tcx.hir().span(node), + cx.tcx.hir_span(node), "local variable doesn't need to be boxed here", ); } diff --git a/src/tools/clippy/clippy_lints/src/index_refutable_slice.rs b/src/tools/clippy/clippy_lints/src/index_refutable_slice.rs index d53e139de014b..989997d69f7c7 100644 --- a/src/tools/clippy/clippy_lints/src/index_refutable_slice.rs +++ b/src/tools/clippy/clippy_lints/src/index_refutable_slice.rs @@ -248,7 +248,7 @@ impl<'tcx> Visitor<'tcx> for SliceIndexLintingVisitor<'_, 'tcx> { { use_info .index_use - .push((index_value, cx.tcx.hir().span(parent_expr.hir_id))); + .push((index_value, cx.tcx.hir_span(parent_expr.hir_id))); return; } diff --git a/src/tools/clippy/clippy_lints/src/loops/mut_range_bound.rs b/src/tools/clippy/clippy_lints/src/loops/mut_range_bound.rs index 5afcf51167d47..a24dd44790e1f 100644 --- a/src/tools/clippy/clippy_lints/src/loops/mut_range_bound.rs +++ b/src/tools/clippy/clippy_lints/src/loops/mut_range_bound.rs @@ -85,10 +85,10 @@ impl<'tcx> Delegate<'tcx> for MutatePairDelegate<'_, 'tcx> { if bk == ty::BorrowKind::Mutable { if let PlaceBase::Local(id) = cmt.place.base { if Some(id) == self.hir_id_low && !BreakAfterExprVisitor::is_found(self.cx, diag_expr_id) { - self.span_low = Some(self.cx.tcx.hir().span(diag_expr_id)); + self.span_low = Some(self.cx.tcx.hir_span(diag_expr_id)); } if Some(id) == self.hir_id_high && !BreakAfterExprVisitor::is_found(self.cx, diag_expr_id) { - self.span_high = Some(self.cx.tcx.hir().span(diag_expr_id)); + self.span_high = Some(self.cx.tcx.hir_span(diag_expr_id)); } } } @@ -97,10 +97,10 @@ impl<'tcx> Delegate<'tcx> for MutatePairDelegate<'_, 'tcx> { fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>, diag_expr_id: HirId) { if let PlaceBase::Local(id) = cmt.place.base { if Some(id) == self.hir_id_low && !BreakAfterExprVisitor::is_found(self.cx, diag_expr_id) { - self.span_low = Some(self.cx.tcx.hir().span(diag_expr_id)); + self.span_low = Some(self.cx.tcx.hir_span(diag_expr_id)); } if Some(id) == self.hir_id_high && !BreakAfterExprVisitor::is_found(self.cx, diag_expr_id) { - self.span_high = Some(self.cx.tcx.hir().span(diag_expr_id)); + self.span_high = Some(self.cx.tcx.hir_span(diag_expr_id)); } } } diff --git a/src/tools/clippy/clippy_lints/src/macro_metavars_in_unsafe.rs b/src/tools/clippy/clippy_lints/src/macro_metavars_in_unsafe.rs index 006addb987f5b..df6e85611fb2c 100644 --- a/src/tools/clippy/clippy_lints/src/macro_metavars_in_unsafe.rs +++ b/src/tools/clippy/clippy_lints/src/macro_metavars_in_unsafe.rs @@ -253,7 +253,7 @@ impl<'tcx> LateLintPass<'tcx> for ExprMetavarsInUnsafe { // Remove the syntax context to hide "in this macro invocation" in the diagnostic. // The invocation doesn't matter. Also we want to dedupe by the unsafe block and not by anything // related to the callsite. - let span = cx.tcx.hir().span(id); + let span = cx.tcx.hir_span(id); (id, Span::new(span.lo(), span.hi(), SyntaxContext::root(), None)) }) diff --git a/src/tools/clippy/clippy_lints/src/misc.rs b/src/tools/clippy/clippy_lints/src/misc.rs index 693d1a8dd7643..09ee6f7037c64 100644 --- a/src/tools/clippy/clippy_lints/src/misc.rs +++ b/src/tools/clippy/clippy_lints/src/misc.rs @@ -329,7 +329,7 @@ fn used_underscore_binding<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { let name = ident.name.as_str(); if name.starts_with('_') && !name.starts_with("__") - && let definition_span = cx.tcx.hir().span(definition_hir_id) + && let definition_span = cx.tcx.hir_span(definition_hir_id) && !definition_span.from_expansion() && !fulfill_or_allowed(cx, USED_UNDERSCORE_BINDING, [expr.hir_id, definition_hir_id]) { diff --git a/src/tools/clippy/clippy_lints/src/needless_pass_by_ref_mut.rs b/src/tools/clippy/clippy_lints/src/needless_pass_by_ref_mut.rs index 576bb27b254c9..b61061d076b74 100644 --- a/src/tools/clippy/clippy_lints/src/needless_pass_by_ref_mut.rs +++ b/src/tools/clippy/clippy_lints/src/needless_pass_by_ref_mut.rs @@ -280,7 +280,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> { diag.span_suggestion( sp, "consider changing to".to_string(), - format!("&{}", snippet(cx, cx.tcx.hir().span(inner_ty.ty.hir_id), "_"),), + format!("&{}", snippet(cx, cx.tcx.hir_span(inner_ty.ty.hir_id), "_"),), Applicability::Unspecified, ); if cx.effective_visibilities.is_exported(*fn_def_id) { diff --git a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs index 55ca875edcee6..978fde33d5170 100644 --- a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs +++ b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs @@ -198,7 +198,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue { // Dereference suggestion let sugg = |diag: &mut Diag<'_, ()>| { if let ty::Adt(def, ..) = ty.kind() { - if let Some(span) = cx.tcx.hir().span_if_local(def.did()) { + if let Some(span) = cx.tcx.hir_span_if_local(def.did()) { if type_allowed_to_implement_copy( cx.tcx, cx.param_env, diff --git a/src/tools/clippy/clippy_lints/src/operators/arithmetic_side_effects.rs b/src/tools/clippy/clippy_lints/src/operators/arithmetic_side_effects.rs index 594101427f5a9..08fcd17e55558 100644 --- a/src/tools/clippy/clippy_lints/src/operators/arithmetic_side_effects.rs +++ b/src/tools/clippy/clippy_lints/src/operators/arithmetic_side_effects.rs @@ -354,7 +354,7 @@ impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects { let body_owner_kind = cx.tcx.hir_body_owner_kind(body_owner_def_id); if let hir::BodyOwnerKind::Const { .. } | hir::BodyOwnerKind::Static(_) = body_owner_kind { - let body_span = cx.tcx.hir().span_with_body(body_owner); + let body_span = cx.tcx.hir_span_with_body(body_owner); if let Some(span) = self.const_span && span.contains(body_span) { @@ -366,7 +366,7 @@ impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects { fn check_body_post(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) { let body_owner = cx.tcx.hir_body_owner(body.id()); - let body_span = cx.tcx.hir().span(body_owner); + let body_span = cx.tcx.hir_span(body_owner); if let Some(span) = self.const_span && span.contains(body_span) { diff --git a/src/tools/clippy/clippy_lints/src/operators/numeric_arithmetic.rs b/src/tools/clippy/clippy_lints/src/operators/numeric_arithmetic.rs index c261fd9bd9cb0..96c9775e292e6 100644 --- a/src/tools/clippy/clippy_lints/src/operators/numeric_arithmetic.rs +++ b/src/tools/clippy/clippy_lints/src/operators/numeric_arithmetic.rs @@ -73,7 +73,7 @@ impl Context { match cx.tcx.hir_body_owner_kind(body_owner_def_id) { hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const { .. } => { - let body_span = cx.tcx.hir().span_with_body(body_owner); + let body_span = cx.tcx.hir_span_with_body(body_owner); if let Some(span) = self.const_span { if span.contains(body_span) { @@ -88,7 +88,7 @@ impl Context { pub fn body_post(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) { let body_owner = cx.tcx.hir_body_owner(body.id()); - let body_span = cx.tcx.hir().span_with_body(body_owner); + let body_span = cx.tcx.hir_span_with_body(body_owner); if let Some(span) = self.const_span { if span.contains(body_span) { diff --git a/src/tools/clippy/clippy_lints/src/shadow.rs b/src/tools/clippy/clippy_lints/src/shadow.rs index ee282ee1dfb71..b82ddedd56c0a 100644 --- a/src/tools/clippy/clippy_lints/src/shadow.rs +++ b/src/tools/clippy/clippy_lints/src/shadow.rs @@ -218,7 +218,7 @@ fn lint_shadow(cx: &LateContext<'_>, pat: &Pat<'_>, shadowed: HirId, span: Span) }, }; span_lint_and_then(cx, lint, span, msg, |diag| { - diag.span_note(cx.tcx.hir().span(shadowed), "previous binding is here"); + diag.span_note(cx.tcx.hir_span(shadowed), "previous binding is here"); }); } diff --git a/src/tools/clippy/clippy_lints/src/single_call_fn.rs b/src/tools/clippy/clippy_lints/src/single_call_fn.rs index 1a2fb77acc15a..64891743dc636 100644 --- a/src/tools/clippy/clippy_lints/src/single_call_fn.rs +++ b/src/tools/clippy/clippy_lints/src/single_call_fn.rs @@ -137,7 +137,7 @@ impl<'tcx> LateLintPass<'tcx> for SingleCallFn { for (&def_id, usage) in &self.def_id_to_usage { if let CallState::Once { call_site } = *usage && let fn_hir_id = cx.tcx.local_def_id_to_hir_id(def_id) - && let fn_span = cx.tcx.hir().span_with_body(fn_hir_id) + && let fn_span = cx.tcx.hir_span_with_body(fn_hir_id) && !self.is_function_allowed(cx, def_id, fn_hir_id, fn_span) { span_lint_hir_and_then( diff --git a/src/tools/clippy/clippy_utils/src/lib.rs b/src/tools/clippy/clippy_utils/src/lib.rs index 668b0cb69e204..8dc28fa3077e0 100644 --- a/src/tools/clippy/clippy_utils/src/lib.rs +++ b/src/tools/clippy/clippy_utils/src/lib.rs @@ -2990,7 +2990,7 @@ pub fn expr_use_ctxt<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> ExprU { adjustments = cx.typeck_results().expr_adjustments(e); } - same_ctxt &= cx.tcx.hir().span(parent_id).ctxt() == ctxt; + same_ctxt &= cx.tcx.hir_span(parent_id).ctxt() == ctxt; if let Node::Expr(e) = parent { match e.kind { ExprKind::If(e, _, _) | ExprKind::Match(e, _, _) if e.hir_id != child_id => { diff --git a/src/tools/clippy/clippy_utils/src/macros.rs b/src/tools/clippy/clippy_utils/src/macros.rs index 1a457bc7f2141..dfb30b9c21864 100644 --- a/src/tools/clippy/clippy_utils/src/macros.rs +++ b/src/tools/clippy/clippy_utils/src/macros.rs @@ -178,7 +178,6 @@ pub fn first_node_in_macro(cx: &LateContext<'_>, node: &impl HirNode) -> Option< // get the parent node, possibly skipping over a statement // if the parent is not found, it is sensible to return `Some(root)` - let hir = cx.tcx.hir(); let mut parent_iter = cx.tcx.hir_parent_iter(node.hir_id()); let (parent_id, _) = match parent_iter.next() { None => return Some(ExpnId::root()), @@ -190,7 +189,7 @@ pub fn first_node_in_macro(cx: &LateContext<'_>, node: &impl HirNode) -> Option< }; // get the macro expansion of the parent node - let parent_span = hir.span(parent_id); + let parent_span = cx.tcx.hir_span(parent_id); let Some(parent_macro_call) = macro_backtrace(parent_span).next() else { // the parent node is not in a macro return Some(ExpnId::root()); diff --git a/src/tools/clippy/clippy_utils/src/sugg.rs b/src/tools/clippy/clippy_utils/src/sugg.rs index 68a1de96a3515..065d48119a24f 100644 --- a/src/tools/clippy/clippy_utils/src/sugg.rs +++ b/src/tools/clippy/clippy_utils/src/sugg.rs @@ -839,8 +839,7 @@ impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> { fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, _: ty::BorrowKind) { if let PlaceBase::Local(id) = cmt.place.base { - let map = self.cx.tcx.hir(); - let span = map.span(cmt.hir_id); + let span = self.cx.tcx.hir_span(cmt.hir_id); let start_span = Span::new(self.next_pos, span.lo(), span.ctxt(), None); let mut start_snip = snippet_with_applicability(self.cx, start_span, "..", &mut self.applicability); diff --git a/tests/run-make/print-crate-root-lint-levels/lib.rs b/tests/run-make/print-crate-root-lint-levels/lib.rs new file mode 100644 index 0000000000000..dc846f8fd9514 --- /dev/null +++ b/tests/run-make/print-crate-root-lint-levels/lib.rs @@ -0,0 +1,5 @@ +#![allow(unexpected_cfgs)] +#![expect(unused_mut)] + +#[deny(unknown_lints)] +mod my_mod {} diff --git a/tests/run-make/print-crate-root-lint-levels/rmake.rs b/tests/run-make/print-crate-root-lint-levels/rmake.rs new file mode 100644 index 0000000000000..e373c91102dfa --- /dev/null +++ b/tests/run-make/print-crate-root-lint-levels/rmake.rs @@ -0,0 +1,118 @@ +//! This checks the output of `--print=crate-root-lint-levels` + +extern crate run_make_support; + +use std::collections::HashSet; +use std::iter::FromIterator; + +use run_make_support::rustc; + +struct CrateRootLintLevels { + args: &'static [&'static str], + contains: Contains, +} + +struct Contains { + contains: &'static [&'static str], + doesnt_contain: &'static [&'static str], +} + +fn main() { + check(CrateRootLintLevels { + args: &[], + contains: Contains { + contains: &[ + "unexpected_cfgs=allow", + "unused_mut=expect", + "warnings=warn", + "stable_features=warn", + "unknown_lints=warn", + ], + doesnt_contain: &["unexpected_cfgs=warn", "unused_mut=warn"], + }, + }); + check(CrateRootLintLevels { + args: &["-Wunexpected_cfgs"], + contains: Contains { + contains: &["unexpected_cfgs=allow", "warnings=warn"], + doesnt_contain: &["unexpected_cfgs=warn"], + }, + }); + check(CrateRootLintLevels { + args: &["-Dwarnings"], + contains: Contains { + contains: &[ + "unexpected_cfgs=allow", + "warnings=deny", + "stable_features=deny", + "unknown_lints=deny", + ], + doesnt_contain: &["warnings=warn"], + }, + }); + check(CrateRootLintLevels { + args: &["-Dstable_features"], + contains: Contains { + contains: &["warnings=warn", "stable_features=deny", "unexpected_cfgs=allow"], + doesnt_contain: &["warnings=deny"], + }, + }); + check(CrateRootLintLevels { + args: &["-Dwarnings", "--force-warn=stable_features"], + contains: Contains { + contains: &["warnings=deny", "stable_features=force-warn", "unknown_lints=deny"], + doesnt_contain: &["warnings=warn"], + }, + }); + check(CrateRootLintLevels { + args: &["-Dwarnings", "--cap-lints=warn"], + contains: Contains { + contains: &[ + "unexpected_cfgs=allow", + "warnings=warn", + "stable_features=warn", + "unknown_lints=warn", + ], + doesnt_contain: &["warnings=deny"], + }, + }); +} + +#[track_caller] +fn check(CrateRootLintLevels { args, contains }: CrateRootLintLevels) { + let output = rustc() + .input("lib.rs") + .arg("-Zunstable-options") + .print("crate-root-lint-levels") + .args(args) + .run(); + + let stdout = output.stdout_utf8(); + + let mut found = HashSet::::new(); + + for l in stdout.lines() { + assert!(l == l.trim()); + if let Some((left, right)) = l.split_once('=') { + assert!(!left.contains("\"")); + assert!(!right.contains("\"")); + } else { + assert!(l.contains('=')); + } + assert!(found.insert(l.to_string()), "{}", &l); + } + + let Contains { contains, doesnt_contain } = contains; + + { + let should_found = HashSet::::from_iter(contains.iter().map(|s| s.to_string())); + let diff: Vec<_> = should_found.difference(&found).collect(); + assert!(diff.is_empty(), "should found: {:?}, didn't found {:?}", &should_found, &diff); + } + { + let should_not_find = + HashSet::::from_iter(doesnt_contain.iter().map(|s| s.to_string())); + let diff: Vec<_> = should_not_find.intersection(&found).collect(); + assert!(diff.is_empty(), "should not find {:?}, did found {:?}", &should_not_find, &diff); + } +} diff --git a/tests/run-make/rustc-help/help-v.stdout b/tests/run-make/rustc-help/help-v.stdout index 98e56735082d1..f19ca1e9f90ab 100644 --- a/tests/run-make/rustc-help/help-v.stdout +++ b/tests/run-make/rustc-help/help-v.stdout @@ -29,7 +29,7 @@ Options: --emit [asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir] Comma separated list of types of output for the compiler to emit - --print [all-target-specs-json|calling-conventions|cfg|check-cfg|code-models|crate-name|deployment-target|file-names|host-tuple|link-args|native-static-libs|relocation-models|split-debuginfo|stack-protector-strategies|supported-crate-types|sysroot|target-cpus|target-features|target-libdir|target-list|target-spec-json|tls-models] + --print [all-target-specs-json|calling-conventions|cfg|check-cfg|code-models|crate-name|crate-root-lint-levels|deployment-target|file-names|host-tuple|link-args|native-static-libs|relocation-models|split-debuginfo|stack-protector-strategies|supported-crate-types|sysroot|target-cpus|target-features|target-libdir|target-list|target-spec-json|tls-models] Compiler information to print on stdout -g Equivalent to -C debuginfo=2 -O Equivalent to -C opt-level=3 diff --git a/tests/run-make/rustc-help/help.stdout b/tests/run-make/rustc-help/help.stdout index 040555f1d04fe..f7d352966035b 100644 --- a/tests/run-make/rustc-help/help.stdout +++ b/tests/run-make/rustc-help/help.stdout @@ -29,7 +29,7 @@ Options: --emit [asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir] Comma separated list of types of output for the compiler to emit - --print [all-target-specs-json|calling-conventions|cfg|check-cfg|code-models|crate-name|deployment-target|file-names|host-tuple|link-args|native-static-libs|relocation-models|split-debuginfo|stack-protector-strategies|supported-crate-types|sysroot|target-cpus|target-features|target-libdir|target-list|target-spec-json|tls-models] + --print [all-target-specs-json|calling-conventions|cfg|check-cfg|code-models|crate-name|crate-root-lint-levels|deployment-target|file-names|host-tuple|link-args|native-static-libs|relocation-models|split-debuginfo|stack-protector-strategies|supported-crate-types|sysroot|target-cpus|target-features|target-libdir|target-list|target-spec-json|tls-models] Compiler information to print on stdout -g Equivalent to -C debuginfo=2 -O Equivalent to -C opt-level=3 diff --git a/tests/ui/invalid-compile-flags/print-without-arg.stderr b/tests/ui/invalid-compile-flags/print-without-arg.stderr index aa8a2ae42db29..8abaee5056bab 100644 --- a/tests/ui/invalid-compile-flags/print-without-arg.stderr +++ b/tests/ui/invalid-compile-flags/print-without-arg.stderr @@ -1,5 +1,5 @@ error: Argument to option 'print' missing Usage: - --print [all-target-specs-json|calling-conventions|cfg|check-cfg|code-models|crate-name|deployment-target|file-names|host-tuple|link-args|native-static-libs|relocation-models|split-debuginfo|stack-protector-strategies|supported-crate-types|sysroot|target-cpus|target-features|target-libdir|target-list|target-spec-json|tls-models] + --print [all-target-specs-json|calling-conventions|cfg|check-cfg|code-models|crate-name|crate-root-lint-levels|deployment-target|file-names|host-tuple|link-args|native-static-libs|relocation-models|split-debuginfo|stack-protector-strategies|supported-crate-types|sysroot|target-cpus|target-features|target-libdir|target-list|target-spec-json|tls-models] Compiler information to print on stdout diff --git a/tests/ui/invalid-compile-flags/print.stderr b/tests/ui/invalid-compile-flags/print.stderr index f9cfb1616ce5a..e3374eb1e6e7e 100644 --- a/tests/ui/invalid-compile-flags/print.stderr +++ b/tests/ui/invalid-compile-flags/print.stderr @@ -1,5 +1,5 @@ error: unknown print request: `yyyy` | - = help: valid print requests are: `all-target-specs-json`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `supported-crate-types`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `tls-models` + = help: valid print requests are: `all-target-specs-json`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `crate-root-lint-levels`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `supported-crate-types`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `tls-models` = help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information diff --git a/tests/ui/print-request/print-lints-help.stderr b/tests/ui/print-request/print-lints-help.stderr index 0530d11f2e802..bc48b2fa73cc7 100644 --- a/tests/ui/print-request/print-lints-help.stderr +++ b/tests/ui/print-request/print-lints-help.stderr @@ -1,6 +1,6 @@ error: unknown print request: `lints` | - = help: valid print requests are: `all-target-specs-json`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `supported-crate-types`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `tls-models` + = help: valid print requests are: `all-target-specs-json`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `crate-root-lint-levels`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `supported-crate-types`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `tls-models` = help: use `-Whelp` to print a list of lints = help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information diff --git a/tests/ui/print-request/stability.rs b/tests/ui/print-request/stability.rs index c3421224d7206..0ec0886e941e7 100644 --- a/tests/ui/print-request/stability.rs +++ b/tests/ui/print-request/stability.rs @@ -18,6 +18,10 @@ //@[all_target_specs_json] compile-flags: --print=all-target-specs-json //@[all_target_specs_json] error-pattern: the `-Z unstable-options` flag must also be passed +//@ revisions: crate_root_lint_levels +//@[crate_root_lint_levels] compile-flags: --print=crate-root-lint-levels +//@[crate_root_lint_levels] error-pattern: the `-Z unstable-options` flag must also be passed + //@ revisions: check_cfg //@[check_cfg] compile-flags: --print=check-cfg //@[check_cfg] error-pattern: the `-Z unstable-options` flag must also be passed