Skip to content

Commit 0c26144

Browse files
Better span for attribute suggestions
`def_span` has the same issues as `body.span`, so do it this way instead.
1 parent 7c6d685 commit 0c26144

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

compiler/rustc_mir/src/transform/check_consts/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ impl ConstCx<'mir, 'tcx> {
5757
&& self.tcx.features().staged_api
5858
&& is_const_stable_const_fn(self.tcx, self.def_id.to_def_id())
5959
}
60+
61+
/// Returns the function signature of the item being const-checked if it is a `fn` or `const fn`.
62+
pub fn fn_sig(&self) -> Option<&'tcx hir::FnSig<'tcx>> {
63+
// Get this from the HIR map instead of a query to avoid cycle errors.
64+
//
65+
// FIXME: Is this still an issue?
66+
let hir_map = self.tcx.hir();
67+
let hir_id = hir_map.local_def_id_to_hir_id(self.def_id);
68+
hir_map.fn_sig_by_hir_id(hir_id)
69+
}
6070
}
6171

6272
/// Returns `true` if this `DefId` points to one of the official `panic` lang items.

compiler/rustc_mir/src/transform/check_consts/validation.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
22
33
use rustc_errors::{struct_span_err, Applicability, Diagnostic};
4-
use rustc_hir::def_id::{DefId, LocalDefId};
4+
use rustc_hir::def_id::DefId;
55
use rustc_hir::{self as hir, HirId, LangItem};
66
use rustc_infer::infer::TyCtxtInferExt;
77
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
@@ -209,7 +209,7 @@ impl Validator<'mir, 'tcx> {
209209

210210
// `async` functions cannot be `const fn`. This is checked during AST lowering, so there's
211211
// no need to emit duplicate errors here.
212-
if is_async_fn(tcx, def_id) || body.generator_kind.is_some() {
212+
if is_async_fn(self.ccx) || body.generator_kind.is_some() {
213213
tcx.sess.delay_span_bug(body.span, "`async` functions cannot be `const fn`");
214214
return;
215215
}
@@ -929,31 +929,29 @@ fn is_int_bool_or_char(ty: Ty<'_>) -> bool {
929929
ty.is_bool() || ty.is_integral() || ty.is_char()
930930
}
931931

932-
fn is_async_fn(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
933-
let hir_map = tcx.hir();
934-
let hir_id = hir_map.local_def_id_to_hir_id(def_id);
935-
hir_map
936-
.fn_sig_by_hir_id(hir_id)
937-
.map_or(false, |sig| sig.header.asyncness == hir::IsAsync::Async)
932+
fn is_async_fn(ccx: &ConstCx<'_, '_>) -> bool {
933+
ccx.fn_sig().map_or(false, |sig| sig.header.asyncness == hir::IsAsync::Async)
938934
}
939935

940936
fn emit_unstable_in_stable_error(ccx: &ConstCx<'_, '_>, span: Span, gate: Symbol) {
937+
let attr_span = ccx.fn_sig().map_or(ccx.body.span, |sig| sig.span.shrink_to_lo());
938+
941939
ccx.tcx
942940
.sess
943941
.struct_span_err(
944942
span,
945943
&format!("const-stable function cannot use `#[feature({})]`", gate.as_str()),
946944
)
947945
.span_suggestion(
948-
ccx.body.span,
946+
attr_span,
949947
"if it is not part of the public API, make this function unstably const",
950948
concat!(r#"#[rustc_const_unstable(feature = "...", issue = "...")]"#, '\n').to_owned(),
951949
Applicability::HasPlaceholders,
952950
)
953951
.span_suggestion(
954-
ccx.body.span,
952+
attr_span,
955953
"otherwise `#[allow_internal_unstable]` can be used to bypass stability checks",
956-
format!("#[allow_internal_unstable({})]", gate),
954+
format!("#[allow_internal_unstable({})]\n", gate),
957955
Applicability::MaybeIncorrect,
958956
)
959957
.emit();

0 commit comments

Comments
 (0)