Skip to content

Commit b567bdb

Browse files
committed
const body span is const item span
1 parent 8f94e48 commit b567bdb

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,7 @@ fn typeck_with_inspect<'tcx>(
118118

119119
let id = tcx.local_def_id_to_hir_id(def_id);
120120
let node = tcx.hir_node(id);
121-
122-
// TODO: yikes
123-
let (def_id_diagnostics, span) = if tcx.def_kind(def_id) == DefKind::AnonConst
124-
&& tcx.anon_const_kind(def_id) == ty::AnonConstKind::ItemBody
125-
{
126-
let const_item = tcx.parent(def_id.to_def_id());
127-
(const_item.expect_local(), tcx.def_span(const_item))
128-
} else {
129-
(def_id, tcx.def_span(def_id))
130-
};
121+
let span = tcx.def_span(def_id);
131122

132123
// Figure out what primary body this item has.
133124
let body_id = node.body_id().unwrap_or_else(|| {
@@ -207,7 +198,7 @@ fn typeck_with_inspect<'tcx>(
207198

208199
let expected_type = fcx.normalize(body.value.span, expected_type);
209200

210-
let wf_code = ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(def_id_diagnostics)));
201+
let wf_code = ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(def_id)));
211202
fcx.register_wf_obligation(expected_type.into(), body.value.span, wf_code);
212203

213204
fcx.check_expr_coercible_to_type(body.value, expected_type, None);

compiler/rustc_middle/src/hir/map.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,9 @@ impl<'tcx> TyCtxt<'tcx> {
984984
// Ensure that the returned span has the item's SyntaxContext.
985985
fn_decl_span.find_ancestor_inside(*span).unwrap_or(*span)
986986
}
987+
Node::AnonConst(anon) if let crate::ty::AnonConstKind::ItemBody = self.anon_const_kind(anon.def_id) => {
988+
return self.hir_span(self.parent_hir_id(self.parent_hir_id(hir_id)));
989+
}
987990
_ => self.hir_span_with_body(hir_id),
988991
};
989992
debug_assert_eq!(span.ctxt(), self.hir_span_with_body(hir_id).ctxt());
@@ -1001,6 +1004,9 @@ impl<'tcx> TyCtxt<'tcx> {
10011004
Node::ImplItem(impl_item) => impl_item.span,
10021005
Node::Variant(variant) => variant.span,
10031006
Node::Field(field) => field.span,
1007+
Node::AnonConst(anon) if let crate::ty::AnonConstKind::ItemBody = self.anon_const_kind(anon.def_id) => {
1008+
return self.hir_span_with_body(self.parent_hir_id(self.parent_hir_id(hir_id)));
1009+
}
10041010
Node::AnonConst(constant) => constant.span,
10051011
Node::ConstBlock(constant) => self.hir_body(constant.body).value.span,
10061012
Node::ConstArg(const_arg) => const_arg.span(),

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ use rustc_span::hygiene::LocalExpnId;
1313
use rustc_span::{Span, Symbol, sym};
1414
use tracing::debug;
1515

16-
use crate::{ImplTraitContext, InvocationParent, Resolver};
16+
use crate::{FxHashMap, ImplTraitContext, InvocationParent, Resolver};
1717

1818
pub(crate) fn collect_definitions(
1919
resolver: &mut Resolver<'_, '_>,
2020
fragment: &AstFragment,
2121
expansion: LocalExpnId,
2222
) {
2323
let invocation_parent = resolver.invocation_parents[&expansion];
24-
let mut visitor = DefCollector { resolver, expansion, invocation_parent };
24+
let mut visitor = DefCollector { resolver, expansion, invocation_parent, overriden_anon_const_spans: FxHashMap::default(), };
2525
fragment.visit_with(&mut visitor);
2626
}
2727

@@ -30,6 +30,7 @@ struct DefCollector<'a, 'ra, 'tcx> {
3030
resolver: &'a mut Resolver<'ra, 'tcx>,
3131
invocation_parent: InvocationParent,
3232
expansion: LocalExpnId,
33+
overriden_anon_const_spans: FxHashMap<NodeId, Span>,
3334
}
3435

3536
impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
@@ -121,7 +122,13 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
121122
mutability: s.mutability,
122123
nested: false,
123124
},
124-
ItemKind::Const(..) => DefKind::Const,
125+
ItemKind::Const(box ConstItem { body, .. }) => {
126+
if let Some(anon) = body {
127+
self.overriden_anon_const_spans.insert(anon.id, i.span);
128+
}
129+
130+
DefKind::Const
131+
}
125132
ItemKind::Fn(..) | ItemKind::Delegation(..) => DefKind::Fn,
126133
ItemKind::MacroDef(ident, def) => {
127134
let edition = i.span.edition();
@@ -338,7 +345,13 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
338345
let (ident, def_kind) = match &i.kind {
339346
AssocItemKind::Fn(box Fn { ident, .. })
340347
| AssocItemKind::Delegation(box Delegation { ident, .. }) => (*ident, DefKind::AssocFn),
341-
AssocItemKind::Const(box ConstItem { ident, .. }) => (*ident, DefKind::AssocConst),
348+
AssocItemKind::Const(box ConstItem { ident, body, .. }) => {
349+
if let Some(anon) = body {
350+
self.overriden_anon_const_spans.insert(anon.id, i.span);
351+
}
352+
353+
(*ident, DefKind::AssocConst)
354+
},
342355
AssocItemKind::Type(box TyAlias { ident, .. }) => (*ident, DefKind::AssocTy),
343356
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
344357
return self.visit_macro_invoc(i.id);
@@ -357,7 +370,10 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
357370
}
358371

359372
fn visit_anon_const(&mut self, constant: &'a AnonConst) {
360-
let parent = self.create_def(constant.id, None, DefKind::AnonConst, constant.value.span);
373+
// TODO: i dont think this actually does anything?? what does this span even correspond to
374+
let span = self.overriden_anon_const_spans.get(&constant.id).copied().unwrap_or(constant.value.span);
375+
376+
let parent = self.create_def(constant.id, None, DefKind::AnonConst, span);
361377
self.with_parent(parent, |this| visit::walk_anon_const(this, constant));
362378
}
363379

0 commit comments

Comments
 (0)