Skip to content

Rollup of 4 pull requests #140366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<'a> State<'a> {
match &_else.kind {
// Another `else if` block.
ast::ExprKind::If(i, then, e) => {
self.cbox(INDENT_UNIT - 1);
self.cbox(0);
self.ibox(0);
self.word(" else if ");
self.print_expr_as_cond(i);
Expand All @@ -30,8 +30,8 @@ impl<'a> State<'a> {
self.print_else(e.as_deref())
}
// Final `else` block.
ast::ExprKind::Block(b, _) => {
self.cbox(INDENT_UNIT - 1);
ast::ExprKind::Block(b, None) => {
self.cbox(0);
self.ibox(0);
self.word(" else ");
self.print_block(b)
Expand All @@ -45,7 +45,9 @@ impl<'a> State<'a> {
}

fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block, elseopt: Option<&ast::Expr>) {
self.head("if");
self.cbox(0);
self.ibox(0);
self.word_nbsp("if");
self.print_expr_as_cond(test);
self.space();
self.print_block(blk);
Expand Down Expand Up @@ -876,6 +878,7 @@ impl<'a> State<'a> {
}
}
} else {
self.end(); // Close the ibox for the pattern.
self.word(",");
}
self.end(); // Close enclosing cbox.
Expand Down
23 changes: 15 additions & 8 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,18 +1064,18 @@ impl<'a> State<'a> {
if let Some(els_inner) = els {
match els_inner.kind {
// Another `else if` block.
hir::ExprKind::If(i, then, e) => {
self.cbox(INDENT_UNIT - 1);
hir::ExprKind::If(i, hir::Expr { kind: hir::ExprKind::Block(t, None), .. }, e) => {
self.cbox(0);
self.ibox(0);
self.word(" else if ");
self.print_expr_as_cond(i);
self.space();
self.print_expr(then);
self.print_block(t);
self.print_else(e);
}
// Final `else` block.
hir::ExprKind::Block(b, _) => {
self.cbox(INDENT_UNIT - 1);
hir::ExprKind::Block(b, None) => {
self.cbox(0);
self.ibox(0);
self.word(" else ");
self.print_block(b);
Expand All @@ -1094,11 +1094,18 @@ impl<'a> State<'a> {
blk: &hir::Expr<'_>,
elseopt: Option<&hir::Expr<'_>>,
) {
self.head("if");
self.cbox(0);
self.ibox(0);
self.word_nbsp("if");
self.print_expr_as_cond(test);
self.space();
self.print_expr(blk);
self.print_else(elseopt)
match blk.kind {
hir::ExprKind::Block(blk, None) => {
self.print_block(blk);
self.print_else(elseopt)
}
_ => panic!("non-block then expr"),
}
}

fn print_anon_const(&mut self, constant: &hir::AnonConst) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ declare_lint! {
///
/// ### Example
///
/// ```rust,compile_fail
/// ```rust,compile_fail,edition2021
/// #[no_mangle]
/// const FOO: i32 = 5;
/// ```
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/impl_trait_overcaptures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ declare_lint! {
///
/// ### Example
///
/// ```rust,compile_fail
/// ```rust,compile_fail,edition2021
/// # #![deny(impl_trait_overcaptures)]
/// # use std::fmt::Display;
/// let mut x = vec![];
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,7 @@ declare_lint! {
///
/// ### Example
///
/// ```rust,compile_fail
/// ```rust,compile_fail,edition2021
/// macro_rules! foo {
/// () => {};
/// ($name) => { };
Expand Down Expand Up @@ -4128,7 +4128,7 @@ declare_lint! {
///
/// ### Example
///
/// ```rust,compile_fail
/// ```rust,compile_fail,edition2021
/// #![deny(dependency_on_unit_never_type_fallback)]
/// fn main() {
/// if true {
Expand Down
32 changes: 20 additions & 12 deletions compiler/rustc_middle/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,26 @@ impl<'tcx> Value<TyCtxt<'tcx>> for &[ty::Variance] {
cycle_error: &CycleError,
_guar: ErrorGuaranteed,
) -> Self {
if let Some(frame) = cycle_error.cycle.get(0)
&& frame.query.dep_kind == dep_kinds::variances_of
&& let Some(def_id) = frame.query.def_id
{
let n = tcx.generics_of(def_id).own_params.len();
vec![ty::Bivariant; n].leak()
} else {
span_bug!(
cycle_error.usage.as_ref().unwrap().0,
"only `variances_of` returns `&[ty::Variance]`"
);
}
search_for_cycle_permutation(
&cycle_error.cycle,
|cycle| {
if let Some(frame) = cycle.get(0)
&& frame.query.dep_kind == dep_kinds::variances_of
&& let Some(def_id) = frame.query.def_id
{
let n = tcx.generics_of(def_id).own_params.len();
ControlFlow::Break(vec![ty::Bivariant; n].leak())
} else {
ControlFlow::Continue(())
}
},
|| {
span_bug!(
cycle_error.usage.as_ref().unwrap().0,
"only `variances_of` returns `&[ty::Variance]`"
)
},
)
}
}

Expand Down
22 changes: 8 additions & 14 deletions src/tools/lint-docs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,21 +444,15 @@ impl<'a> LintExtractor<'a> {
fs::write(&tempfile, source)
.map_err(|e| format!("failed to write {}: {}", tempfile.display(), e))?;
let mut cmd = Command::new(self.rustc_path);
if options.contains(&"edition2024") {
cmd.arg("--edition=2024");
cmd.arg("-Zunstable-options");
} else if options.contains(&"edition2021") {
cmd.arg("--edition=2021");
} else if options.contains(&"edition2018") {
cmd.arg("--edition=2018");
} else if options.contains(&"edition2015") {
cmd.arg("--edition=2015");
} else if options.contains(&"edition") {
panic!("lint-docs: unknown edition");
} else {
let edition = options
.iter()
.filter_map(|opt| opt.strip_prefix("edition"))
.next()
// defaults to latest edition
cmd.arg("--edition=2021");
}
.unwrap_or("2024");
cmd.arg(format!("--edition={edition}"));
// Just in case this is an unstable edition.
cmd.arg("-Zunstable-options");
cmd.arg("--error-format=json");
cmd.arg("--target").arg(self.rustc_target);
if let Some(target_linker) = self.rustc_linker {
Expand Down
39 changes: 39 additions & 0 deletions tests/pretty/hir-if-else.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
//@ pretty-compare-only
//@ pretty-mode:hir
//@ pp-exact:hir-if-else.pp

fn f(x: u32,
y:
u32) {
let mut a = 0;
if x > y { a = 1; } else { a = 2; }

if x < 1 {
a = 1;
} else if x < 2 {
a = 2;
} else if x < 3 { a = 3; } else if x < 4 { a = 4; } else { a = 5; }

if x < y {
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
} else { a += 1; a += 1; a += 1; a += 1; a += 1; a += 1; }

if x < 1 {
if x < 2 {
if x < 3 {
a += 1;
} else if x < 4 { a += 1; if x < 5 { a += 1; } }
} else if x < 6 { a += 1; }
}
}

fn main() { f(3, 4); }
59 changes: 59 additions & 0 deletions tests/pretty/hir-if-else.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//@ pretty-compare-only
//@ pretty-mode:hir
//@ pp-exact:hir-if-else.pp

fn f(x: u32, y: u32) {
let mut a = 0;
if x > y {
a = 1;
} else {
a = 2;
}

if x < 1 {
a = 1;
} else if x < 2 {
a = 2;
} else if x < 3 {
a = 3;
} else if x < 4 {
a = 4;
} else {
a = 5;
}

if x < y {
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
} else {
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
}

if x < 1 {
if x < 2 {
if x < 3 {
a += 1;
} else if x < 4 {
a += 1;
if x < 5 {
a += 1;
}
}
} else if x < 6 {
a += 1;
}
}
}

fn main() {
f(3, 4);
}
52 changes: 52 additions & 0 deletions tests/pretty/if-else.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
//@ pretty-compare-only
//@ pretty-mode:expanded
//@ pp-exact:if-else.pp

fn f(x: u32, y: u32) {
let mut a = 0;
if x > y { a = 1; } else { a = 2; }

if x < 1 {
a = 1;
} else if x < 2 {
a = 2;
} else if x < 3 { a = 3; } else if x < 4 { a = 4; } else { a = 5; }

if x < y {
a += 1;
a += 1;
a += 1;
} else {
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
a += 1;
}

if x < 1 {
if x < 2 {
if x < 3 {
a += 1;
} else if x < 4 { a += 1; if x < 5 { a += 1; } }
} else if x < 6 { a += 1; }
}
}

fn main() { f(3, 4); }
Loading
Loading