Skip to content

Commit 45263fc

Browse files
committed
Auto merge of rust-lang#99054 - Dylan-DPC:rollup-0zuhhds, r=Dylan-DPC
Rollup of 4 pull requests Successful merges: - rust-lang#98533 (Add a `-Zdump-drop-tracking-cfg` debugging flag) - rust-lang#98654 (An optimization for `pest-2.1.3`) - rust-lang#98657 (Migrate some diagnostics from `rustc_const_eval` to `SessionDiagnostic`) - rust-lang#98794 (Highlight conflicting param-env candidates) Failed merges: - rust-lang#98957 ( don't allow ZST in ScalarInt ) r? `@ghost` `@rustbot` modify labels: rollup
2 parents fbdb07f + 54dde86 commit 45263fc

File tree

28 files changed

+372
-145
lines changed

28 files changed

+372
-145
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3813,6 +3813,7 @@ dependencies = [
38133813
"atty",
38143814
"rustc_data_structures",
38153815
"rustc_error_messages",
3816+
"rustc_hir",
38163817
"rustc_lint_defs",
38173818
"rustc_macros",
38183819
"rustc_serialize",
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use rustc_hir::ConstContext;
2+
use rustc_macros::SessionDiagnostic;
3+
use rustc_span::Span;
4+
5+
#[derive(SessionDiagnostic)]
6+
#[error(const_eval::unstable_in_stable)]
7+
pub(crate) struct UnstableInStable {
8+
pub gate: String,
9+
#[primary_span]
10+
pub span: Span,
11+
#[suggestion(
12+
const_eval::unstable_sugg,
13+
code = "#[rustc_const_unstable(feature = \"...\", issue = \"...\")]\n",
14+
applicability = "has-placeholders"
15+
)]
16+
#[suggestion(
17+
const_eval::bypass_sugg,
18+
code = "#[rustc_allow_const_fn_unstable({gate})]\n",
19+
applicability = "has-placeholders"
20+
)]
21+
pub attr_span: Span,
22+
}
23+
24+
#[derive(SessionDiagnostic)]
25+
#[error(const_eval::thread_local_access, code = "E0625")]
26+
pub(crate) struct NonConstOpErr {
27+
#[primary_span]
28+
pub span: Span,
29+
}
30+
31+
#[derive(SessionDiagnostic)]
32+
#[error(const_eval::static_access, code = "E0013")]
33+
#[help]
34+
pub(crate) struct StaticAccessErr {
35+
#[primary_span]
36+
pub span: Span,
37+
pub kind: ConstContext,
38+
#[note(const_eval::teach_note)]
39+
#[help(const_eval::teach_help)]
40+
pub teach: Option<()>,
41+
}
42+
43+
#[derive(SessionDiagnostic)]
44+
#[error(const_eval::raw_ptr_to_int)]
45+
#[note]
46+
#[note(const_eval::note2)]
47+
pub(crate) struct RawPtrToIntErr {
48+
#[primary_span]
49+
pub span: Span,
50+
}
51+
52+
#[derive(SessionDiagnostic)]
53+
#[error(const_eval::raw_ptr_comparison)]
54+
#[note]
55+
pub(crate) struct RawPtrComparisonErr {
56+
#[primary_span]
57+
pub span: Span,
58+
}
59+
60+
#[derive(SessionDiagnostic)]
61+
#[error(const_eval::panic_non_str)]
62+
pub(crate) struct PanicNonStrErr {
63+
#[primary_span]
64+
pub span: Span,
65+
}
66+
67+
#[derive(SessionDiagnostic)]
68+
#[error(const_eval::mut_deref, code = "E0658")]
69+
pub(crate) struct MutDerefErr {
70+
#[primary_span]
71+
pub span: Span,
72+
pub kind: ConstContext,
73+
}
74+
75+
#[derive(SessionDiagnostic)]
76+
#[error(const_eval::transient_mut_borrow, code = "E0658")]
77+
pub(crate) struct TransientMutBorrowErr {
78+
#[primary_span]
79+
pub span: Span,
80+
pub kind: ConstContext,
81+
}
82+
83+
#[derive(SessionDiagnostic)]
84+
#[error(const_eval::transient_mut_borrow_raw, code = "E0658")]
85+
pub(crate) struct TransientMutBorrowErrRaw {
86+
#[primary_span]
87+
pub span: Span,
88+
pub kind: ConstContext,
89+
}

compiler/rustc_const_eval/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern crate tracing;
3131
extern crate rustc_middle;
3232

3333
pub mod const_eval;
34+
mod errors;
3435
pub mod interpret;
3536
pub mod transform;
3637
pub mod util;

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
22
3-
use rustc_errors::{Applicability, Diagnostic, ErrorGuaranteed};
3+
use rustc_errors::{Diagnostic, ErrorGuaranteed};
44
use rustc_hir as hir;
55
use rustc_hir::def_id::DefId;
66
use rustc_index::bit_set::BitSet;
@@ -24,6 +24,7 @@ use super::qualifs::{self, CustomEq, HasMutInterior, NeedsDrop, NeedsNonConstDro
2424
use super::resolver::FlowSensitiveAnalysis;
2525
use super::{ConstCx, Qualif};
2626
use crate::const_eval::is_unstable_const_fn;
27+
use crate::errors::UnstableInStable;
2728

2829
type QualifResults<'mir, 'tcx, Q> =
2930
rustc_mir_dataflow::ResultsCursor<'mir, 'tcx, FlowSensitiveAnalysis<'mir, 'mir, 'tcx, Q>>;
@@ -1026,23 +1027,5 @@ fn is_int_bool_or_char(ty: Ty<'_>) -> bool {
10261027
fn emit_unstable_in_stable_error(ccx: &ConstCx<'_, '_>, span: Span, gate: Symbol) {
10271028
let attr_span = ccx.tcx.def_span(ccx.def_id()).shrink_to_lo();
10281029

1029-
ccx.tcx
1030-
.sess
1031-
.struct_span_err(
1032-
span,
1033-
&format!("const-stable function cannot use `#[feature({})]`", gate.as_str()),
1034-
)
1035-
.span_suggestion(
1036-
attr_span,
1037-
"if it is not part of the public API, make this function unstably const",
1038-
concat!(r#"#[rustc_const_unstable(feature = "...", issue = "...")]"#, '\n'),
1039-
Applicability::HasPlaceholders,
1040-
)
1041-
.span_suggestion(
1042-
attr_span,
1043-
"otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks",
1044-
format!("#[rustc_allow_const_fn_unstable({})]\n", gate),
1045-
Applicability::MaybeIncorrect,
1046-
)
1047-
.emit();
1030+
ccx.tcx.sess.emit_err(UnstableInStable { gate: gate.to_string(), span, attr_span });
10481031
}

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+29-64
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! Concrete error types for all operations which may be invalid in a certain const context.
22
33
use hir::def_id::LocalDefId;
4-
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
4+
use rustc_errors::{
5+
error_code, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed,
6+
};
57
use rustc_hir as hir;
68
use rustc_hir::def_id::DefId;
79
use rustc_infer::infer::TyCtxtInferExt;
@@ -20,6 +22,10 @@ use rustc_span::{BytePos, Pos, Span, Symbol};
2022
use rustc_trait_selection::traits::SelectionContext;
2123

2224
use super::ConstCx;
25+
use crate::errors::{
26+
MutDerefErr, NonConstOpErr, PanicNonStrErr, RawPtrComparisonErr, RawPtrToIntErr,
27+
StaticAccessErr, TransientMutBorrowErr, TransientMutBorrowErrRaw,
28+
};
2329
use crate::util::{call_kind, CallDesugaringKind, CallKind};
2430

2531
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -590,17 +596,17 @@ impl<'tcx> NonConstOp<'tcx> for TransientMutBorrow {
590596
ccx: &ConstCx<'_, 'tcx>,
591597
span: Span,
592598
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
593-
let raw = match self.0 {
594-
hir::BorrowKind::Raw => "raw ",
595-
hir::BorrowKind::Ref => "",
596-
};
597-
598-
feature_err(
599-
&ccx.tcx.sess.parse_sess,
600-
sym::const_mut_refs,
601-
span,
602-
&format!("{}mutable references are not allowed in {}s", raw, ccx.const_kind()),
603-
)
599+
let kind = ccx.const_kind();
600+
match self.0 {
601+
hir::BorrowKind::Raw => ccx
602+
.tcx
603+
.sess
604+
.create_feature_err(TransientMutBorrowErrRaw { span, kind }, sym::const_mut_refs),
605+
hir::BorrowKind::Ref => ccx
606+
.tcx
607+
.sess
608+
.create_feature_err(TransientMutBorrowErr { span, kind }, sym::const_mut_refs),
609+
}
604610
}
605611
}
606612

@@ -621,12 +627,9 @@ impl<'tcx> NonConstOp<'tcx> for MutDeref {
621627
ccx: &ConstCx<'_, 'tcx>,
622628
span: Span,
623629
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
624-
feature_err(
625-
&ccx.tcx.sess.parse_sess,
626-
sym::const_mut_refs,
627-
span,
628-
&format!("mutation through a reference is not allowed in {}s", ccx.const_kind()),
629-
)
630+
ccx.tcx
631+
.sess
632+
.create_feature_err(MutDerefErr { span, kind: ccx.const_kind() }, sym::const_mut_refs)
630633
}
631634
}
632635

@@ -639,10 +642,7 @@ impl<'tcx> NonConstOp<'tcx> for PanicNonStr {
639642
ccx: &ConstCx<'_, 'tcx>,
640643
span: Span,
641644
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
642-
ccx.tcx.sess.struct_span_err(
643-
span,
644-
"argument to `panic!()` in a const context must have type `&str`",
645-
)
645+
ccx.tcx.sess.create_err(PanicNonStrErr { span })
646646
}
647647
}
648648

@@ -657,15 +657,7 @@ impl<'tcx> NonConstOp<'tcx> for RawPtrComparison {
657657
ccx: &ConstCx<'_, 'tcx>,
658658
span: Span,
659659
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
660-
let mut err = ccx
661-
.tcx
662-
.sess
663-
.struct_span_err(span, "pointers cannot be reliably compared during const eval");
664-
err.note(
665-
"see issue #53020 <https://github.com/rust-lang/rust/issues/53020> \
666-
for more information",
667-
);
668-
err
660+
ccx.tcx.sess.create_err(RawPtrComparisonErr { span })
669661
}
670662
}
671663

@@ -701,15 +693,7 @@ impl<'tcx> NonConstOp<'tcx> for RawPtrToIntCast {
701693
ccx: &ConstCx<'_, 'tcx>,
702694
span: Span,
703695
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
704-
let mut err = ccx
705-
.tcx
706-
.sess
707-
.struct_span_err(span, "pointers cannot be cast to integers during const eval");
708-
err.note("at compile-time, pointers do not have an integer value");
709-
err.note(
710-
"avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior",
711-
);
712-
err
696+
ccx.tcx.sess.create_err(RawPtrToIntErr { span })
713697
}
714698
}
715699

@@ -730,24 +714,11 @@ impl<'tcx> NonConstOp<'tcx> for StaticAccess {
730714
ccx: &ConstCx<'_, 'tcx>,
731715
span: Span,
732716
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
733-
let mut err = struct_span_err!(
734-
ccx.tcx.sess,
717+
ccx.tcx.sess.create_err(StaticAccessErr {
735718
span,
736-
E0013,
737-
"{}s cannot refer to statics",
738-
ccx.const_kind()
739-
);
740-
err.help(
741-
"consider extracting the value of the `static` to a `const`, and referring to that",
742-
);
743-
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
744-
err.note(
745-
"`static` and `const` variables can refer to other `const` variables. \
746-
A `const` variable, however, cannot refer to a `static` variable.",
747-
);
748-
err.help("To fix this, the value can be extracted to a `const` and then used.");
749-
}
750-
err
719+
kind: ccx.const_kind(),
720+
teach: ccx.tcx.sess.teach(&error_code!(E0013)).then_some(()),
721+
})
751722
}
752723
}
753724

@@ -760,13 +731,7 @@ impl<'tcx> NonConstOp<'tcx> for ThreadLocalAccess {
760731
ccx: &ConstCx<'_, 'tcx>,
761732
span: Span,
762733
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
763-
struct_span_err!(
764-
ccx.tcx.sess,
765-
span,
766-
E0625,
767-
"thread-local statics cannot be \
768-
accessed at compile-time"
769-
)
734+
ccx.tcx.sess.create_err(NonConstOpErr { span })
770735
}
771736
}
772737

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
builtin_macros-requires-cfg-pattern =
1+
builtin-macros-requires-cfg-pattern =
22
macro requires a cfg-pattern as an argument
33
.label = cfg-pattern required
44
5-
builtin_macros-expected-one-cfg-pattern = expected 1 cfg-pattern
5+
builtin-macros-expected-one-cfg-pattern = expected 1 cfg-pattern
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const-eval-unstable-in-stable =
2+
const-stable function cannot use `#[feature({$gate})]`
3+
.unstable-sugg = if it is not part of the public API, make this function unstably const
4+
.bypass-sugg = otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
5+
6+
const-eval-thread-local-access =
7+
thread-local statics cannot be accessed at compile-time
8+
9+
const-eval-static-access =
10+
{$kind}s cannot refer to statics
11+
.help = consider extracting the value of the `static` to a `const`, and referring to that
12+
.teach-note = `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
13+
.teach-help = To fix this, the value can be extracted to a `const` and then used.
14+
15+
const-eval-raw-ptr-to-int =
16+
pointers cannot be cast to integers during const eval
17+
.note = at compile-time, pointers do not have an integer value
18+
.note2 = avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior
19+
20+
const-eval-raw-ptr-comparison =
21+
pointers cannot be reliably compared during const eval
22+
.note = see issue #53020 <https://github.com/rust-lang/rust/issues/53020> for more information
23+
24+
const-eval-panic-non-str = argument to `panic!()` in a const context must have type `&str`
25+
26+
const-eval-mut-deref =
27+
mutation through a reference is not allowed in {$kind}s
28+
29+
const-eval-transient-mut-borrow = mutable references are not allowed in {$kind}s
30+
31+
const-eval-transient-mut-borrow-raw = raw mutable references are not allowed in {$kind}s

compiler/rustc_error_messages/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ fluent_messages! {
3737
parser => "../locales/en-US/parser.ftl",
3838
privacy => "../locales/en-US/privacy.ftl",
3939
typeck => "../locales/en-US/typeck.ftl",
40+
const_eval => "../locales/en-US/const_eval.ftl",
4041
}
4142

4243
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};

compiler/rustc_errors/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rustc_serialize = { path = "../rustc_serialize" }
1313
rustc_span = { path = "../rustc_span" }
1414
rustc_macros = { path = "../rustc_macros" }
1515
rustc_data_structures = { path = "../rustc_data_structures" }
16+
rustc_hir = { path = "../rustc_hir" }
1617
rustc_lint_defs = { path = "../rustc_lint_defs" }
1718
unicode-width = "0.1.4"
1819
atty = "0.2"

compiler/rustc_errors/src/diagnostic.rs

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
};
66
use rustc_data_structures::stable_map::FxHashMap;
77
use rustc_error_messages::FluentValue;
8+
use rustc_hir as hir;
89
use rustc_lint_defs::{Applicability, LintExpectationId};
910
use rustc_span::edition::LATEST_STABLE_EDITION;
1011
use rustc_span::symbol::{Ident, Symbol};
@@ -160,6 +161,16 @@ impl<'source> Into<FluentValue<'source>> for DiagnosticArgValue<'source> {
160161
}
161162
}
162163

164+
impl IntoDiagnosticArg for hir::ConstContext {
165+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
166+
DiagnosticArgValue::Str(Cow::Borrowed(match self {
167+
hir::ConstContext::ConstFn => "constant function",
168+
hir::ConstContext::Static(_) => "static",
169+
hir::ConstContext::Const => "constant",
170+
}))
171+
}
172+
}
173+
163174
/// Trait implemented by error types. This should not be implemented manually. Instead, use
164175
/// `#[derive(SessionSubdiagnostic)]` -- see [rustc_macros::SessionSubdiagnostic].
165176
#[rustc_diagnostic_item = "AddSubdiagnostic"]

compiler/rustc_hir/src/hir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,9 @@ impl fmt::Display for ConstContext {
15951595
}
15961596
}
15971597

1598+
// NOTE: `IntoDiagnosticArg` impl for `ConstContext` lives in `rustc_errors`
1599+
// due to a cyclical dependency between hir that crate.
1600+
15981601
/// A literal.
15991602
pub type Lit = Spanned<LitKind>;
16001603

0 commit comments

Comments
 (0)