|
1 | 1 | //! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
|
2 | 2 |
|
3 | 3 | use rustc_errors::{struct_span_err, Applicability, Diagnostic};
|
4 |
| -use rustc_hir::def_id::{DefId, LocalDefId}; |
| 4 | +use rustc_hir::def_id::DefId; |
5 | 5 | use rustc_hir::{self as hir, HirId, LangItem};
|
6 | 6 | use rustc_infer::infer::TyCtxtInferExt;
|
7 | 7 | use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
|
@@ -209,7 +209,7 @@ impl Validator<'mir, 'tcx> {
|
209 | 209 |
|
210 | 210 | // `async` functions cannot be `const fn`. This is checked during AST lowering, so there's
|
211 | 211 | // 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() { |
213 | 213 | tcx.sess.delay_span_bug(body.span, "`async` functions cannot be `const fn`");
|
214 | 214 | return;
|
215 | 215 | }
|
@@ -929,31 +929,29 @@ fn is_int_bool_or_char(ty: Ty<'_>) -> bool {
|
929 | 929 | ty.is_bool() || ty.is_integral() || ty.is_char()
|
930 | 930 | }
|
931 | 931 |
|
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) |
938 | 934 | }
|
939 | 935 |
|
940 | 936 | 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 | + |
941 | 939 | ccx.tcx
|
942 | 940 | .sess
|
943 | 941 | .struct_span_err(
|
944 | 942 | span,
|
945 | 943 | &format!("const-stable function cannot use `#[feature({})]`", gate.as_str()),
|
946 | 944 | )
|
947 | 945 | .span_suggestion(
|
948 |
| - ccx.body.span, |
| 946 | + attr_span, |
949 | 947 | "if it is not part of the public API, make this function unstably const",
|
950 | 948 | concat!(r#"#[rustc_const_unstable(feature = "...", issue = "...")]"#, '\n').to_owned(),
|
951 | 949 | Applicability::HasPlaceholders,
|
952 | 950 | )
|
953 | 951 | .span_suggestion(
|
954 |
| - ccx.body.span, |
| 952 | + attr_span, |
955 | 953 | "otherwise `#[allow_internal_unstable]` can be used to bypass stability checks",
|
956 |
| - format!("#[allow_internal_unstable({})]", gate), |
| 954 | + format!("#[allow_internal_unstable({})]\n", gate), |
957 | 955 | Applicability::MaybeIncorrect,
|
958 | 956 | )
|
959 | 957 | .emit();
|
|
0 commit comments