Skip to content

Commit 51748a8

Browse files
committed
Auto merge of #82816 - GuillaumeGomez:rollup-hxohu2e, r=GuillaumeGomez
Rollup of 7 pull requests Successful merges: - #80845 (Make ItemKind::ExternCrate looks like hir::ItemKind::ExternCrate to make transition over hir::ItemKind simpler) - #82708 (Warn on `#![doc(test(...))]` on items other than the crate root and use future incompatible lint) - #82714 (Detect match arm body without braces) - #82736 (Bump optimization from mir_opt_level 2 to 3 and 3 to 4 and make "release" be level 2 by default) - #82782 (Make rustc shim's verbose output include crate_name being compiled.) - #82797 (Update tests names to start with `issue-`) - #82809 (rustdoc: Use substrings instead of split to grab enum variant paths) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents caca212 + 8dfbc00 commit 51748a8

File tree

113 files changed

+665
-188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+665
-188
lines changed

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ fn test_debugging_options_tracking_hash() {
566566
tracked!(link_only, true);
567567
tracked!(merge_functions, Some(MergeFunctions::Disabled));
568568
tracked!(mir_emit_retag, true);
569-
tracked!(mir_opt_level, 3);
569+
tracked!(mir_opt_level, Some(4));
570570
tracked!(mutable_noalias, true);
571571
tracked!(new_llvm_pass_manager, true);
572572
tracked!(no_codegen, true);

compiler/rustc_lint_defs/src/builtin.rs

+30
Original file line numberDiff line numberDiff line change
@@ -3059,3 +3059,33 @@ declare_lint! {
30593059
Allow,
30603060
"No declared ABI for extern declaration"
30613061
}
3062+
3063+
declare_lint! {
3064+
/// The `invalid_doc_attributes` lint detects when the `#[doc(...)]` is
3065+
/// misused.
3066+
///
3067+
/// ### Example
3068+
///
3069+
/// ```rust,compile_fail
3070+
/// #![deny(warnings)]
3071+
///
3072+
/// pub mod submodule {
3073+
/// #![doc(test(no_crate_inject))]
3074+
/// }
3075+
/// ```
3076+
///
3077+
/// {{produces}}
3078+
///
3079+
/// ### Explanation
3080+
///
3081+
/// Previously, there were very like checks being performed on `#[doc(..)]`
3082+
/// unlike the other attributes. It'll now catch all the issues that it
3083+
/// silently ignored previously.
3084+
pub INVALID_DOC_ATTRIBUTES,
3085+
Warn,
3086+
"detects invalid `#[doc(...)]` attributes",
3087+
@future_incompatible = FutureIncompatibleInfo {
3088+
reference: "issue #82730 <https://github.com/rust-lang/rust/issues/82730>",
3089+
edition: None,
3090+
};
3091+
}

compiler/rustc_mir/src/transform/const_goto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct ConstGoto;
2828

2929
impl<'tcx> MirPass<'tcx> for ConstGoto {
3030
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
31-
if tcx.sess.opts.debugging_opts.mir_opt_level < 3 {
31+
if tcx.sess.mir_opt_level() < 4 {
3232
return;
3333
}
3434
trace!("Running ConstGoto on {:?}", body.source);

compiler/rustc_mir/src/transform/const_prop.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
725725
return None;
726726
}
727727

728-
if self.tcx.sess.opts.debugging_opts.mir_opt_level >= 3 {
728+
if self.tcx.sess.mir_opt_level() >= 4 {
729729
self.eval_rvalue_with_identities(rvalue, place)
730730
} else {
731731
self.use_ecx(|this| this.ecx.eval_rvalue_into_place(rvalue, place))
@@ -903,7 +903,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
903903

904904
/// Returns `true` if and only if this `op` should be const-propagated into.
905905
fn should_const_prop(&mut self, op: &OpTy<'tcx>) -> bool {
906-
let mir_opt_level = self.tcx.sess.opts.debugging_opts.mir_opt_level;
906+
let mir_opt_level = self.tcx.sess.mir_opt_level();
907907

908908
if mir_opt_level == 0 {
909909
return false;
@@ -1071,9 +1071,9 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
10711071
fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
10721072
self.super_operand(operand, location);
10731073

1074-
// Only const prop copies and moves on `mir_opt_level=2` as doing so
1074+
// Only const prop copies and moves on `mir_opt_level=3` as doing so
10751075
// currently slightly increases compile time in some cases.
1076-
if self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
1076+
if self.tcx.sess.mir_opt_level() >= 3 {
10771077
self.propagate_operand(operand)
10781078
}
10791079
}
@@ -1253,7 +1253,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
12531253
TerminatorKind::SwitchInt { ref mut discr, .. } => {
12541254
// FIXME: This is currently redundant with `visit_operand`, but sadly
12551255
// always visiting operands currently causes a perf regression in LLVM codegen, so
1256-
// `visit_operand` currently only runs for propagates places for `mir_opt_level=3`.
1256+
// `visit_operand` currently only runs for propagates places for `mir_opt_level=4`.
12571257
self.propagate_operand(discr)
12581258
}
12591259
// None of these have Operands to const-propagate.
@@ -1272,7 +1272,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
12721272
// Every argument in our function calls have already been propagated in `visit_operand`.
12731273
//
12741274
// NOTE: because LLVM codegen gives slight performance regressions with it, so this is
1275-
// gated on `mir_opt_level=2`.
1275+
// gated on `mir_opt_level=3`.
12761276
TerminatorKind::Call { .. } => {}
12771277
}
12781278

compiler/rustc_mir/src/transform/deduplicate_blocks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct DeduplicateBlocks;
1616

1717
impl<'tcx> MirPass<'tcx> for DeduplicateBlocks {
1818
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
19-
if tcx.sess.opts.debugging_opts.mir_opt_level < 3 {
19+
if tcx.sess.mir_opt_level() < 4 {
2020
return;
2121
}
2222
debug!("Running DeduplicateBlocks on `{:?}`", body.source);

compiler/rustc_mir/src/transform/dest_prop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ pub struct DestinationPropagation;
127127

128128
impl<'tcx> MirPass<'tcx> for DestinationPropagation {
129129
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
130-
// Only run at mir-opt-level=2 or higher for now (we don't fix up debuginfo and remove
130+
// Only run at mir-opt-level=3 or higher for now (we don't fix up debuginfo and remove
131131
// storage statements at the moment).
132-
if tcx.sess.opts.debugging_opts.mir_opt_level <= 1 {
132+
if tcx.sess.mir_opt_level() < 3 {
133133
return;
134134
}
135135

compiler/rustc_mir/src/transform/early_otherwise_branch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct EarlyOtherwiseBranch;
2626

2727
impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
2828
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
29-
if tcx.sess.opts.debugging_opts.mir_opt_level < 2 {
29+
if tcx.sess.mir_opt_level() < 3 {
3030
return;
3131
}
3232
trace!("running EarlyOtherwiseBranch on {:?}", body.source);

compiler/rustc_mir/src/transform/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ crate fn is_enabled(tcx: TyCtxt<'_>) -> bool {
5252
return enabled;
5353
}
5454

55-
tcx.sess.opts.debugging_opts.mir_opt_level >= 2
55+
tcx.sess.mir_opt_level() >= 3
5656
}
5757

5858
impl<'tcx> MirPass<'tcx> for Inline {

compiler/rustc_mir/src/transform/match_branches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct MatchBranchSimplification;
4040
4141
impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
4242
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
43-
if tcx.sess.opts.debugging_opts.mir_opt_level <= 1 {
43+
if tcx.sess.mir_opt_level() < 3 {
4444
return;
4545
}
4646

compiler/rustc_mir/src/transform/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc
475475
}
476476

477477
fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
478-
let mir_opt_level = tcx.sess.opts.debugging_opts.mir_opt_level;
478+
let mir_opt_level = tcx.sess.mir_opt_level();
479479

480480
// Lowering generator control-flow and variables has to happen before we do anything else
481481
// to them. We run some optimizations before that, because they may be harder to do on the state

compiler/rustc_mir/src/transform/multiple_return_terminators.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct MultipleReturnTerminators;
1010

1111
impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
1212
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
13-
if tcx.sess.opts.debugging_opts.mir_opt_level < 3 {
13+
if tcx.sess.mir_opt_level() < 4 {
1414
return;
1515
}
1616

compiler/rustc_mir/src/transform/nrvo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct RenameReturnPlace;
3434

3535
impl<'tcx> MirPass<'tcx> for RenameReturnPlace {
3636
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) {
37-
if tcx.sess.opts.debugging_opts.mir_opt_level == 0 {
37+
if tcx.sess.mir_opt_level() == 0 {
3838
return;
3939
}
4040

compiler/rustc_mir/src/transform/unreachable_prop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub struct UnreachablePropagation;
1212

1313
impl MirPass<'_> for UnreachablePropagation {
1414
fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
15-
if tcx.sess.opts.debugging_opts.mir_opt_level < 3 {
16-
// Enable only under -Zmir-opt-level=3 as in some cases (check the deeply-nested-opt
15+
if tcx.sess.mir_opt_level() < 4 {
16+
// Enable only under -Zmir-opt-level=4 as in some cases (check the deeply-nested-opt
1717
// perf benchmark) LLVM may spend quite a lot of time optimizing the generated code.
1818
return;
1919
}

compiler/rustc_parse/src/parser/expr.rs

+111
Original file line numberDiff line numberDiff line change
@@ -1973,6 +1973,102 @@ impl<'a> Parser<'a> {
19731973
Ok(self.mk_expr(lo.to(hi), ExprKind::Match(scrutinee, arms), attrs))
19741974
}
19751975

1976+
/// Attempt to recover from match arm body with statements and no surrounding braces.
1977+
fn parse_arm_body_missing_braces(
1978+
&mut self,
1979+
first_expr: &P<Expr>,
1980+
arrow_span: Span,
1981+
) -> Option<P<Expr>> {
1982+
if self.token.kind != token::Semi {
1983+
return None;
1984+
}
1985+
let start_snapshot = self.clone();
1986+
let semi_sp = self.token.span;
1987+
self.bump(); // `;`
1988+
let mut stmts =
1989+
vec![self.mk_stmt(first_expr.span, ast::StmtKind::Expr(first_expr.clone()))];
1990+
let err = |this: &mut Parser<'_>, stmts: Vec<ast::Stmt>| {
1991+
let span = stmts[0].span.to(stmts[stmts.len() - 1].span);
1992+
let mut err = this.struct_span_err(span, "`match` arm body without braces");
1993+
let (these, s, are) =
1994+
if stmts.len() > 1 { ("these", "s", "are") } else { ("this", "", "is") };
1995+
err.span_label(
1996+
span,
1997+
&format!(
1998+
"{these} statement{s} {are} not surrounded by a body",
1999+
these = these,
2000+
s = s,
2001+
are = are
2002+
),
2003+
);
2004+
err.span_label(arrow_span, "while parsing the `match` arm starting here");
2005+
if stmts.len() > 1 {
2006+
err.multipart_suggestion(
2007+
&format!("surround the statement{} with a body", s),
2008+
vec![
2009+
(span.shrink_to_lo(), "{ ".to_string()),
2010+
(span.shrink_to_hi(), " }".to_string()),
2011+
],
2012+
Applicability::MachineApplicable,
2013+
);
2014+
} else {
2015+
err.span_suggestion(
2016+
semi_sp,
2017+
"use a comma to end a `match` arm expression",
2018+
",".to_string(),
2019+
Applicability::MachineApplicable,
2020+
);
2021+
}
2022+
err.emit();
2023+
this.mk_expr_err(span)
2024+
};
2025+
// We might have either a `,` -> `;` typo, or a block without braces. We need
2026+
// a more subtle parsing strategy.
2027+
loop {
2028+
if self.token.kind == token::CloseDelim(token::Brace) {
2029+
// We have reached the closing brace of the `match` expression.
2030+
return Some(err(self, stmts));
2031+
}
2032+
if self.token.kind == token::Comma {
2033+
*self = start_snapshot;
2034+
return None;
2035+
}
2036+
let pre_pat_snapshot = self.clone();
2037+
match self.parse_pat_no_top_alt(None) {
2038+
Ok(_pat) => {
2039+
if self.token.kind == token::FatArrow {
2040+
// Reached arm end.
2041+
*self = pre_pat_snapshot;
2042+
return Some(err(self, stmts));
2043+
}
2044+
}
2045+
Err(mut err) => {
2046+
err.cancel();
2047+
}
2048+
}
2049+
2050+
*self = pre_pat_snapshot;
2051+
match self.parse_stmt_without_recovery(true, ForceCollect::No) {
2052+
// Consume statements for as long as possible.
2053+
Ok(Some(stmt)) => {
2054+
stmts.push(stmt);
2055+
}
2056+
Ok(None) => {
2057+
*self = start_snapshot;
2058+
break;
2059+
}
2060+
// We couldn't parse either yet another statement missing it's
2061+
// enclosing block nor the next arm's pattern or closing brace.
2062+
Err(mut stmt_err) => {
2063+
stmt_err.cancel();
2064+
*self = start_snapshot;
2065+
break;
2066+
}
2067+
}
2068+
}
2069+
None
2070+
}
2071+
19762072
pub(super) fn parse_arm(&mut self) -> PResult<'a, Arm> {
19772073
let attrs = self.parse_outer_attributes()?;
19782074
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
@@ -2007,6 +2103,21 @@ impl<'a> Parser<'a> {
20072103

20082104
if require_comma {
20092105
let sm = this.sess.source_map();
2106+
if let Some(body) = this.parse_arm_body_missing_braces(&expr, arrow_span) {
2107+
let span = body.span;
2108+
return Ok((
2109+
ast::Arm {
2110+
attrs,
2111+
pat,
2112+
guard,
2113+
body,
2114+
span,
2115+
id: DUMMY_NODE_ID,
2116+
is_placeholder: false,
2117+
},
2118+
TrailingToken::None,
2119+
));
2120+
}
20102121
this.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)]).map_err(
20112122
|mut err| {
20122123
match (sm.span_to_lines(expr.span), sm.span_to_lines(arm_start_span)) {

compiler/rustc_parse/src/parser/stmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<'a> Parser<'a> {
3434

3535
/// If `force_capture` is true, forces collection of tokens regardless of whether
3636
/// or not we have attributes
37-
fn parse_stmt_without_recovery(
37+
crate fn parse_stmt_without_recovery(
3838
&mut self,
3939
capture_semi: bool,
4040
force_collect: ForceCollect,

compiler/rustc_passes/src/check_attr.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use rustc_hir::{
1717
self, FnSig, ForeignItem, ForeignItemKind, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID,
1818
};
1919
use rustc_hir::{MethodKind, Target};
20-
use rustc_session::lint::builtin::{CONFLICTING_REPR_HINTS, UNUSED_ATTRIBUTES};
20+
use rustc_session::lint::builtin::{
21+
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, UNUSED_ATTRIBUTES,
22+
};
2123
use rustc_session::parse::feature_err;
2224
use rustc_span::symbol::{sym, Symbol};
2325
use rustc_span::{Span, DUMMY_SP};
@@ -544,6 +546,21 @@ impl CheckAttrVisitor<'tcx> {
544546
{
545547
return false;
546548
}
549+
} else if meta.has_name(sym::test) {
550+
if CRATE_HIR_ID != hir_id {
551+
self.tcx.struct_span_lint_hir(
552+
INVALID_DOC_ATTRIBUTES,
553+
hir_id,
554+
meta.span(),
555+
|lint| {
556+
lint.build(
557+
"`#![doc(test(...)]` is only allowed as a crate level attribute"
558+
)
559+
.emit();
560+
},
561+
);
562+
return false;
563+
}
547564
} else if let Some(i_meta) = meta.meta_item() {
548565
if ![
549566
sym::cfg,
@@ -568,19 +585,14 @@ impl CheckAttrVisitor<'tcx> {
568585
.any(|m| i_meta.has_name(*m))
569586
{
570587
self.tcx.struct_span_lint_hir(
571-
UNUSED_ATTRIBUTES,
588+
INVALID_DOC_ATTRIBUTES,
572589
hir_id,
573590
i_meta.span,
574591
|lint| {
575592
lint.build(&format!(
576593
"unknown `doc` attribute `{}`",
577594
i_meta.name_or_empty()
578595
))
579-
.warn(
580-
"this was previously accepted by the compiler but is \
581-
being phased out; it will become a hard error in \
582-
a future release!",
583-
)
584596
.emit();
585597
},
586598
);

0 commit comments

Comments
 (0)