Skip to content

Commit 0887113

Browse files
committed
Auto merge of #98307 - matthiaskrgr:rollup-rb3huha, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - #98235 (Drop magic value 3 from code) - #98267 (Don't omit comma when suggesting wildcard arm after macro expr) - #98276 (Mention formatting macros when encountering `ArgumentV1` method in const) - #98296 (Add a link to the unstable book page on Generator doc comment) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 5750a6a + dfa933d commit 0887113

File tree

20 files changed

+361
-281
lines changed

20 files changed

+361
-281
lines changed

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use rustc_middle::mir;
1010
use rustc_middle::ty::print::with_no_trimmed_paths;
1111
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
1212
use rustc_middle::ty::{
13-
suggest_constraining_type_param, Adt, Closure, FnDef, FnPtr, Param, TraitPredicate, Ty,
13+
suggest_constraining_type_param, Adt, Closure, DefIdTree, FnDef, FnPtr, Param, TraitPredicate,
14+
Ty,
1415
};
1516
use rustc_middle::ty::{Binder, BoundConstness, ImplPolarity, TraitRef};
1617
use rustc_session::parse::feature_err;
@@ -300,6 +301,15 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
300301
diag_trait(&mut err, self_ty, tcx.lang_items().deref_trait().unwrap());
301302
err
302303
}
304+
_ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::ArgumentV1Methods) => {
305+
struct_span_err!(
306+
ccx.tcx.sess,
307+
span,
308+
E0015,
309+
"cannot call non-const formatting macro in {}s",
310+
ccx.const_kind(),
311+
)
312+
}
303313
_ => struct_span_err!(
304314
ccx.tcx.sess,
305315
span,

compiler/rustc_middle/src/mir/spanview.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ fn trim_span_hi(span: Span, to_pos: BytePos) -> Span {
667667
fn fn_span<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Span {
668668
let fn_decl_span = tcx.def_span(def_id);
669669
if let Some(body_span) = hir_body(tcx, def_id).map(|hir_body| hir_body.value.span) {
670-
if fn_decl_span.ctxt() == body_span.ctxt() { fn_decl_span.to(body_span) } else { body_span }
670+
if fn_decl_span.eq_ctxt(body_span) { fn_decl_span.to(body_span) } else { body_span }
671671
} else {
672672
fn_decl_span
673673
}

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ fn non_exhaustive_match<'p, 'tcx>(
803803
let mut suggestion = None;
804804
let sm = cx.tcx.sess.source_map();
805805
match arms {
806-
[] if sp.ctxt() == expr_span.ctxt() => {
806+
[] if sp.eq_ctxt(expr_span) => {
807807
// Get the span for the empty match body `{}`.
808808
let (indentation, more) = if let Some(snippet) = sm.indentation_before(sp) {
809809
(format!("\n{}", snippet), " ")
@@ -821,24 +821,36 @@ fn non_exhaustive_match<'p, 'tcx>(
821821
));
822822
}
823823
[only] => {
824-
let pre_indentation = if let (Some(snippet), true) = (
825-
sm.indentation_before(only.span),
826-
sm.is_multiline(sp.shrink_to_hi().with_hi(only.span.lo())),
827-
) {
828-
format!("\n{}", snippet)
824+
let (pre_indentation, is_multiline) = if let Some(snippet) = sm.indentation_before(only.span)
825+
&& let Ok(with_trailing) = sm.span_extend_while(only.span, |c| c.is_whitespace() || c == ',')
826+
&& sm.is_multiline(with_trailing)
827+
{
828+
(format!("\n{}", snippet), true)
829+
} else {
830+
(" ".to_string(), false)
831+
};
832+
let comma = if matches!(only.body.kind, hir::ExprKind::Block(..))
833+
&& only.span.eq_ctxt(only.body.span)
834+
&& is_multiline
835+
{
836+
""
829837
} else {
830-
" ".to_string()
838+
","
831839
};
832-
let comma = if matches!(only.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
833840
suggestion = Some((
834841
only.span.shrink_to_hi(),
835842
format!("{}{}{} => todo!()", comma, pre_indentation, pattern),
836843
));
837844
}
838-
[.., prev, last] if prev.span.ctxt() == last.span.ctxt() => {
845+
[.., prev, last] if prev.span.eq_ctxt(last.span) => {
839846
if let Ok(snippet) = sm.span_to_snippet(prev.span.between(last.span)) {
840-
let comma =
841-
if matches!(last.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
847+
let comma = if matches!(last.body.kind, hir::ExprKind::Block(..))
848+
&& last.span.eq_ctxt(last.body.span)
849+
{
850+
""
851+
} else {
852+
","
853+
};
842854
suggestion = Some((
843855
last.span.shrink_to_hi(),
844856
format!(

compiler/rustc_mir_transform/src/coverage/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
121121

122122
let source_file = source_map.lookup_source_file(body_span.lo());
123123
let fn_sig_span = match some_fn_sig.filter(|fn_sig| {
124-
fn_sig.span.ctxt() == body_span.ctxt()
124+
fn_sig.span.eq_ctxt(body_span)
125125
&& Lrc::ptr_eq(&source_file, &source_map.lookup_source_file(fn_sig.span.lo()))
126126
}) {
127127
Some(fn_sig) => fn_sig.span.with_hi(body_span.lo()),

compiler/rustc_mir_transform/src/coverage/spans.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl CoverageSpan {
195195
.expn_span
196196
.parent_callsite()
197197
.unwrap_or_else(|| bug!("macro must have a parent"))
198-
.ctxt() == body_span.ctxt()
198+
.eq_ctxt(body_span)
199199
{
200200
return Some(current_macro);
201201
}

compiler/rustc_mir_transform/src/generator.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ const RETURNED: usize = GeneratorSubsts::RETURNED;
195195
/// Generator has panicked and is poisoned.
196196
const POISONED: usize = GeneratorSubsts::POISONED;
197197

198+
/// Number of variants to reserve in generator state. Corresponds to
199+
/// `UNRESUMED` (beginning of a generator) and `RETURNED`/`POISONED`
200+
/// (end of a generator) states.
201+
const RESERVED_VARIANTS: usize = 3;
202+
198203
/// A `yield` point in the generator.
199204
struct SuspensionPoint<'tcx> {
200205
/// State discriminant used when suspending or resuming at this point.
@@ -345,7 +350,7 @@ impl<'tcx> MutVisitor<'tcx> for TransformVisitor<'tcx> {
345350
data.statements.extend(self.make_state(state_idx, v, source_info));
346351
let state = if let Some((resume, mut resume_arg)) = resume {
347352
// Yield
348-
let state = 3 + self.suspension_points.len();
353+
let state = RESERVED_VARIANTS + self.suspension_points.len();
349354

350355
// The resume arg target location might itself be remapped if its base local is
351356
// live across a yield.
@@ -792,7 +797,6 @@ fn compute_layout<'tcx>(
792797
// Leave empty variants for the UNRESUMED, RETURNED, and POISONED states.
793798
// In debuginfo, these will correspond to the beginning (UNRESUMED) or end
794799
// (RETURNED, POISONED) of the function.
795-
const RESERVED_VARIANTS: usize = 3;
796800
let body_span = body.source_scopes[OUTERMOST_SOURCE_SCOPE].span;
797801
let mut variant_source_info: IndexVec<VariantIdx, SourceInfo> = [
798802
SourceInfo::outermost(body_span.shrink_to_lo()),

compiler/rustc_resolve/src/late/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1894,7 +1894,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
18941894
let names = rib
18951895
.bindings
18961896
.iter()
1897-
.filter(|(id, _)| id.span.ctxt() == label.span.ctxt())
1897+
.filter(|(id, _)| id.span.eq_ctxt(label.span))
18981898
.map(|(id, _)| id.name)
18991899
.collect::<Vec<Symbol>>();
19001900

compiler/rustc_span/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,9 @@ impl Span {
537537
pub fn ctxt(self) -> SyntaxContext {
538538
self.data_untracked().ctxt
539539
}
540+
pub fn eq_ctxt(self, other: Span) -> bool {
541+
self.data_untracked().ctxt == other.data_untracked().ctxt
542+
}
540543
#[inline]
541544
pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
542545
self.data_untracked().with_ctxt(ctxt)

compiler/rustc_span/src/symbol.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ symbols! {
131131
Arc,
132132
Argument,
133133
ArgumentV1,
134+
ArgumentV1Methods,
134135
Arguments,
135136
AsMut,
136137
AsRef,
@@ -1641,7 +1642,7 @@ impl Ident {
16411642

16421643
impl PartialEq for Ident {
16431644
fn eq(&self, rhs: &Self) -> bool {
1644-
self.name == rhs.name && self.span.ctxt() == rhs.span.ctxt()
1645+
self.name == rhs.name && self.span.eq_ctxt(rhs.span)
16451646
}
16461647
}
16471648

library/core/src/fmt/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ macro_rules! arg_new {
320320
};
321321
}
322322

323+
#[rustc_diagnostic_item = "ArgumentV1Methods"]
323324
impl<'a> ArgumentV1<'a> {
324325
#[doc(hidden)]
325326
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]

library/core/src/ops/generator.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ pub enum GeneratorState<Y, R> {
6161
/// }
6262
/// ```
6363
///
64-
/// More documentation of generators can be found in the unstable book.
64+
/// More documentation of generators can be found in the [unstable book].
6565
///
6666
/// [RFC 2033]: https://github.com/rust-lang/rfcs/pull/2033
67+
/// [unstable book]: ../../unstable-book/language-features/generators.html
6768
#[lang = "generator"]
6869
#[unstable(feature = "generator_trait", issue = "43122")]
6970
#[fundamental]
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const fn failure() {
2+
panic!("{:?}", 0);
3+
//~^ ERROR cannot call non-const formatting macro in constant functions
4+
//~| ERROR erroneous constant used
5+
//~| ERROR erroneous constant used
6+
//~| WARN this was previously accepted by the compiler
7+
//~| WARN this was previously accepted by the compiler
8+
}
9+
10+
const fn print() {
11+
println!("{:?}", 0);
12+
//~^ ERROR cannot call non-const formatting macro in constant functions
13+
//~| ERROR `Arguments::<'a>::new_v1` is not yet stable as a const fn
14+
//~| ERROR cannot call non-const fn `_print` in constant functions
15+
//~| ERROR erroneous constant used
16+
//~| ERROR erroneous constant used
17+
//~| WARN this was previously accepted by the compiler
18+
//~| WARN this was previously accepted by the compiler
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
error[E0015]: cannot call non-const formatting macro in constant functions
2+
--> $DIR/format.rs:2:20
3+
|
4+
LL | panic!("{:?}", 0);
5+
| ^
6+
|
7+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
8+
= note: this error originates in the macro `$crate::const_format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
9+
10+
error[E0015]: cannot call non-const formatting macro in constant functions
11+
--> $DIR/format.rs:11:22
12+
|
13+
LL | println!("{:?}", 0);
14+
| ^
15+
|
16+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
17+
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
18+
19+
error: `Arguments::<'a>::new_v1` is not yet stable as a const fn
20+
--> $DIR/format.rs:11:5
21+
|
22+
LL | println!("{:?}", 0);
23+
| ^^^^^^^^^^^^^^^^^^^
24+
|
25+
= help: add `#![feature(const_fmt_arguments_new)]` to the crate attributes to enable
26+
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
27+
28+
error[E0015]: cannot call non-const fn `_print` in constant functions
29+
--> $DIR/format.rs:11:5
30+
|
31+
LL | println!("{:?}", 0);
32+
| ^^^^^^^^^^^^^^^^^^^
33+
|
34+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
35+
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
36+
37+
error: erroneous constant used
38+
--> $DIR/format.rs:2:12
39+
|
40+
LL | panic!("{:?}", 0);
41+
| ^^^^^^ referenced constant has errors
42+
|
43+
= note: `#[deny(const_err)]` on by default
44+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
45+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
46+
47+
error: erroneous constant used
48+
--> $DIR/format.rs:2:20
49+
|
50+
LL | panic!("{:?}", 0);
51+
| ^ referenced constant has errors
52+
|
53+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
54+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
55+
= note: this error originates in the macro `$crate::const_format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
56+
57+
error: erroneous constant used
58+
--> $DIR/format.rs:11:14
59+
|
60+
LL | println!("{:?}", 0);
61+
| ^^^^^^ referenced constant has errors
62+
|
63+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
64+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
65+
66+
error: erroneous constant used
67+
--> $DIR/format.rs:11:22
68+
|
69+
LL | println!("{:?}", 0);
70+
| ^ referenced constant has errors
71+
|
72+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
73+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
74+
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
75+
76+
error: aborting due to 8 previous errors
77+
78+
For more information about this error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)