Rollup merge of #129318 - GuillaumeGomez:rm-unneeded-defid-conversion, r=notriddle

Remove unneeded conversion to `DefId` for `ExtraInfo`

I'm working on adding support for "unit test doctests" and this first cleanup came up so just sending it ahead of the rest.

r? ``@notriddle``
This commit is contained in:
Matthias Krüger 2024-08-20 22:22:00 +02:00 committed by GitHub
commit f6312870a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 31 additions and 28 deletions

View File

@ -12,7 +12,7 @@ use rustc_attr::{ConstStability, Deprecation, Stability, StabilityLevel, StableS
use rustc_const_eval::const_eval::is_unstable_const_fn;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{BodyId, Mutability};
use rustc_hir_analysis::check::intrinsic::intrinsic_operation_unsafety;
@ -88,6 +88,11 @@ impl ItemId {
}
}
#[inline]
pub(crate) fn as_local_def_id(self) -> Option<LocalDefId> {
self.as_def_id().and_then(|id| id.as_local())
}
#[inline]
pub(crate) fn krate(self) -> CrateNum {
match self {

View File

@ -136,7 +136,7 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> {
self.enable_per_target_ignores,
Some(&crate::html::markdown::ExtraInfo::new(
self.tcx,
def_id.to_def_id(),
def_id,
span_of_fragments(&attrs.doc_strings).unwrap_or(sp),
)),
);

View File

@ -40,7 +40,7 @@ use pulldown_cmark::{
};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{Diag, DiagMessage};
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::LocalDefId;
use rustc_middle::ty::TyCtxt;
pub(crate) use rustc_resolve::rustdoc::main_body_opts;
use rustc_resolve::rustdoc::may_be_doc_link;
@ -818,38 +818,35 @@ pub(crate) fn find_codes<T: doctest::DocTestVisitor>(
}
pub(crate) struct ExtraInfo<'tcx> {
def_id: DefId,
def_id: LocalDefId,
sp: Span,
tcx: TyCtxt<'tcx>,
}
impl<'tcx> ExtraInfo<'tcx> {
pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: DefId, sp: Span) -> ExtraInfo<'tcx> {
pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId, sp: Span) -> ExtraInfo<'tcx> {
ExtraInfo { def_id, sp, tcx }
}
fn error_invalid_codeblock_attr(&self, msg: impl Into<DiagMessage>) {
if let Some(def_id) = self.def_id.as_local() {
self.tcx.node_span_lint(
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
self.tcx.local_def_id_to_hir_id(def_id),
self.tcx.local_def_id_to_hir_id(self.def_id),
self.sp,
|lint| {
lint.primary_message(msg);
},
);
}
}
fn error_invalid_codeblock_attr_with_help(
&self,
msg: impl Into<DiagMessage>,
f: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
) {
if let Some(def_id) = self.def_id.as_local() {
self.tcx.node_span_lint(
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
self.tcx.local_def_id_to_hir_id(def_id),
self.tcx.local_def_id_to_hir_id(self.def_id),
self.sp,
|lint| {
lint.primary_message(msg);
@ -858,7 +855,6 @@ impl<'tcx> ExtraInfo<'tcx> {
);
}
}
}
#[derive(Eq, PartialEq, Clone, Debug)]
pub(crate) struct LangString {

View File

@ -16,9 +16,11 @@ use crate::core::DocContext;
use crate::html::markdown::{self, RustCodeBlock};
pub(crate) fn visit_item(cx: &DocContext<'_>, item: &clean::Item) {
if let Some(dox) = &item.opt_doc_value() {
if let Some(def_id) = item.item_id.as_local_def_id()
&& let Some(dox) = &item.opt_doc_value()
{
let sp = item.attr_span(cx.tcx);
let extra = crate::html::markdown::ExtraInfo::new(cx.tcx, item.item_id.expect_def_id(), sp);
let extra = crate::html::markdown::ExtraInfo::new(cx.tcx, def_id, sp);
for code_block in markdown::rust_code_blocks(dox, &extra) {
check_rust_syntax(cx, item, dox, code_block);
}