Skip to content

Commit caf9131

Browse files
committed
Query span as late as possible
1 parent c9b606e commit caf9131

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

Diff for: compiler/rustc_codegen_llvm/src/consts.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc_middle::mir::mono::MonoItem;
1919
use rustc_middle::ty::{self, Instance, Ty};
2020
use rustc_middle::{bug, span_bug};
2121
use rustc_span::symbol::sym;
22-
use rustc_span::Span;
2322
use rustc_target::abi::{AddressSpace, Align, HasDataLayout, LayoutOf, Primitive, Scalar, Size};
2423
use tracing::debug;
2524

@@ -110,7 +109,7 @@ fn check_and_apply_linkage(
110109
attrs: &CodegenFnAttrs,
111110
ty: Ty<'tcx>,
112111
sym: &str,
113-
span: Span,
112+
span_def_id: DefId,
114113
) -> &'ll Value {
115114
let llty = cx.layout_of(ty).llvm_type(cx);
116115
if let Some(linkage) = attrs.linkage {
@@ -125,7 +124,7 @@ fn check_and_apply_linkage(
125124
cx.layout_of(mt.ty).llvm_type(cx)
126125
} else {
127126
cx.sess().span_fatal(
128-
span,
127+
cx.tcx.def_span(span_def_id),
129128
"must have type `*const T` or `*mut T` due to `#[linkage]` attribute",
130129
)
131130
};
@@ -143,7 +142,10 @@ fn check_and_apply_linkage(
143142
let mut real_name = "_rust_extern_with_linkage_".to_string();
144143
real_name.push_str(&sym);
145144
let g2 = cx.define_global(&real_name, llty).unwrap_or_else(|| {
146-
cx.sess().span_fatal(span, &format!("symbol `{}` is already defined", &sym))
145+
cx.sess().span_fatal(
146+
cx.tcx.def_span(span_def_id),
147+
&format!("symbol `{}` is already defined", &sym),
148+
)
147149
});
148150
llvm::LLVMRustSetLinkage(g2, llvm::Linkage::InternalLinkage);
149151
llvm::LLVMSetInitializer(g2, g1);
@@ -210,21 +212,21 @@ impl CodegenCx<'ll, 'tcx> {
210212

211213
debug!("get_static: sym={} instance={:?}", sym, instance);
212214

213-
let g = if let Some(def_id) = def_id.as_local() {
214-
let id = self.tcx.hir().local_def_id_to_hir_id(def_id);
215+
let g = if let Some(local_def_id) = def_id.as_local() {
216+
let id = self.tcx.hir().local_def_id_to_hir_id(local_def_id);
215217
let llty = self.layout_of(ty).llvm_type(self);
216218
// FIXME: refactor this to work without accessing the HIR
217219
let (g, attrs) = match self.tcx.hir().get(id) {
218-
Node::Item(&hir::Item { attrs, span, kind: hir::ItemKind::Static(..), .. }) => {
220+
Node::Item(&hir::Item { attrs, kind: hir::ItemKind::Static(..), .. }) => {
219221
if let Some(g) = self.get_declared_value(sym) {
220222
if self.val_ty(g) != self.type_ptr_to(llty) {
221-
span_bug!(span, "Conflicting types for static");
223+
span_bug!(self.tcx.def_span(def_id), "Conflicting types for static");
222224
}
223225
}
224226

225227
let g = self.declare_global(sym, llty);
226228

227-
if !self.tcx.is_reachable_non_generic(def_id) {
229+
if !self.tcx.is_reachable_non_generic(local_def_id) {
228230
unsafe {
229231
llvm::LLVMRustSetVisibility(g, llvm::Visibility::Hidden);
230232
}
@@ -235,12 +237,11 @@ impl CodegenCx<'ll, 'tcx> {
235237

236238
Node::ForeignItem(&hir::ForeignItem {
237239
ref attrs,
238-
span,
239240
kind: hir::ForeignItemKind::Static(..),
240241
..
241242
}) => {
242-
let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
243-
(check_and_apply_linkage(&self, &fn_attrs, ty, sym, span), &**attrs)
243+
let fn_attrs = self.tcx.codegen_fn_attrs(local_def_id);
244+
(check_and_apply_linkage(&self, &fn_attrs, ty, sym, def_id), &**attrs)
244245
}
245246

246247
item => bug!("get_static: expected static, found {:?}", item),
@@ -260,8 +261,7 @@ impl CodegenCx<'ll, 'tcx> {
260261
debug!("get_static: sym={} item_attr={:?}", sym, self.tcx.item_attrs(def_id));
261262

262263
let attrs = self.tcx.codegen_fn_attrs(def_id);
263-
let span = self.tcx.def_span(def_id);
264-
let g = check_and_apply_linkage(&self, &attrs, ty, sym, span);
264+
let g = check_and_apply_linkage(&self, &attrs, ty, sym, def_id);
265265

266266
// Thread-local statics in some other crate need to *always* be linked
267267
// against in a thread-local fashion, so we need to be sure to apply the

Diff for: compiler/rustc_codegen_ssa/src/base.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use rustc_session::cgu_reuse_tracker::CguReuse;
4646
use rustc_session::config::{self, EntryFnType};
4747
use rustc_session::utils::NativeLibKind;
4848
use rustc_session::Session;
49-
use rustc_span::Span;
5049
use rustc_symbol_mangling::test as symbol_names_test;
5150
use rustc_target::abi::{Align, LayoutOf, VariantIdx};
5251

@@ -364,11 +363,7 @@ pub fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
364363
pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
365364
cx: &'a Bx::CodegenCx,
366365
) -> Option<Bx::Function> {
367-
let (main_def_id, span) = match cx.tcx().entry_fn(LOCAL_CRATE) {
368-
Some((def_id, _)) => (def_id, cx.tcx().def_span(def_id)),
369-
None => return None,
370-
};
371-
366+
let main_def_id = cx.tcx().entry_fn(LOCAL_CRATE).map(|(def_id, _)| def_id)?;
372367
let instance = Instance::mono(cx.tcx(), main_def_id.to_def_id());
373368

374369
if !cx.codegen_unit().contains_item(&MonoItem::Fn(instance)) {
@@ -381,12 +376,11 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
381376

382377
return cx.tcx().entry_fn(LOCAL_CRATE).map(|(_, et)| {
383378
let use_start_lang_item = EntryFnType::Start != et;
384-
create_entry_fn::<Bx>(cx, span, main_llfn, main_def_id, use_start_lang_item)
379+
create_entry_fn::<Bx>(cx, main_llfn, main_def_id, use_start_lang_item)
385380
});
386381

387382
fn create_entry_fn<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
388383
cx: &'a Bx::CodegenCx,
389-
sp: Span,
390384
rust_main: Bx::Value,
391385
rust_main_def_id: LocalDefId,
392386
use_start_lang_item: bool,
@@ -411,8 +405,9 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
411405
Some(llfn) => llfn,
412406
None => {
413407
// FIXME: We should be smart and show a better diagnostic here.
408+
let span = cx.tcx().def_span(rust_main_def_id);
414409
cx.sess()
415-
.struct_span_err(sp, "entry symbol `main` declared multiple times")
410+
.struct_span_err(span, "entry symbol `main` declared multiple times")
416411
.help("did you use `#[no_mangle]` on `fn main`? Use `#[start]` instead")
417412
.emit();
418413
cx.sess().abort_if_errors();

0 commit comments

Comments
 (0)