Auto merge of #140366 - matthiaskrgr:rollup-zd3q1oy, r=matthiaskrgr

Rollup of 4 pull requests

Successful merges:

 - #140246 (Fix never pattern printing)
 - #140280 (Improve if/else pretty printing)
 - #140348 (Update lint-docs to default to Rust 2024)
 - #140358 (Use `search_for_cycle_permutation` to look for `variances_of`)

r? `@ghost`
`@rustbot` modify labels: rollup
This commit is contained in:
bors 2025-04-27 16:39:04 +00:00
commit cb31a009e3
16 changed files with 368 additions and 108 deletions

View file

@ -21,7 +21,7 @@ impl<'a> State<'a> {
match &_else.kind { match &_else.kind {
// Another `else if` block. // Another `else if` block.
ast::ExprKind::If(i, then, e) => { ast::ExprKind::If(i, then, e) => {
self.cbox(INDENT_UNIT - 1); self.cbox(0);
self.ibox(0); self.ibox(0);
self.word(" else if "); self.word(" else if ");
self.print_expr_as_cond(i); self.print_expr_as_cond(i);
@ -30,8 +30,8 @@ impl<'a> State<'a> {
self.print_else(e.as_deref()) self.print_else(e.as_deref())
} }
// Final `else` block. // Final `else` block.
ast::ExprKind::Block(b, _) => { ast::ExprKind::Block(b, None) => {
self.cbox(INDENT_UNIT - 1); self.cbox(0);
self.ibox(0); self.ibox(0);
self.word(" else "); self.word(" else ");
self.print_block(b) self.print_block(b)
@ -45,7 +45,9 @@ impl<'a> State<'a> {
} }
fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block, elseopt: Option<&ast::Expr>) { 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.print_expr_as_cond(test);
self.space(); self.space();
self.print_block(blk); self.print_block(blk);
@ -876,6 +878,7 @@ impl<'a> State<'a> {
} }
} }
} else { } else {
self.end(); // Close the ibox for the pattern.
self.word(","); self.word(",");
} }
self.end(); // Close enclosing cbox. self.end(); // Close enclosing cbox.

View file

@ -1064,18 +1064,18 @@ impl<'a> State<'a> {
if let Some(els_inner) = els { if let Some(els_inner) = els {
match els_inner.kind { match els_inner.kind {
// Another `else if` block. // Another `else if` block.
hir::ExprKind::If(i, then, e) => { hir::ExprKind::If(i, hir::Expr { kind: hir::ExprKind::Block(t, None), .. }, e) => {
self.cbox(INDENT_UNIT - 1); self.cbox(0);
self.ibox(0); self.ibox(0);
self.word(" else if "); self.word(" else if ");
self.print_expr_as_cond(i); self.print_expr_as_cond(i);
self.space(); self.space();
self.print_expr(then); self.print_block(t);
self.print_else(e); self.print_else(e);
} }
// Final `else` block. // Final `else` block.
hir::ExprKind::Block(b, _) => { hir::ExprKind::Block(b, None) => {
self.cbox(INDENT_UNIT - 1); self.cbox(0);
self.ibox(0); self.ibox(0);
self.word(" else "); self.word(" else ");
self.print_block(b); self.print_block(b);
@ -1094,11 +1094,18 @@ impl<'a> State<'a> {
blk: &hir::Expr<'_>, blk: &hir::Expr<'_>,
elseopt: Option<&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.print_expr_as_cond(test);
self.space(); self.space();
self.print_expr(blk); match blk.kind {
self.print_else(elseopt) 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) { fn print_anon_const(&mut self, constant: &hir::AnonConst) {

View file

@ -948,7 +948,7 @@ declare_lint! {
/// ///
/// ### Example /// ### Example
/// ///
/// ```rust,compile_fail /// ```rust,compile_fail,edition2021
/// #[no_mangle] /// #[no_mangle]
/// const FOO: i32 = 5; /// const FOO: i32 = 5;
/// ``` /// ```

View file

@ -41,7 +41,7 @@ declare_lint! {
/// ///
/// ### Example /// ### Example
/// ///
/// ```rust,compile_fail /// ```rust,compile_fail,edition2021
/// # #![deny(impl_trait_overcaptures)] /// # #![deny(impl_trait_overcaptures)]
/// # use std::fmt::Display; /// # use std::fmt::Display;
/// let mut x = vec![]; /// let mut x = vec![];

View file

@ -1424,7 +1424,7 @@ declare_lint! {
/// ///
/// ### Example /// ### Example
/// ///
/// ```rust,compile_fail /// ```rust,compile_fail,edition2021
/// macro_rules! foo { /// macro_rules! foo {
/// () => {}; /// () => {};
/// ($name) => { }; /// ($name) => { };
@ -4128,7 +4128,7 @@ declare_lint! {
/// ///
/// ### Example /// ### Example
/// ///
/// ```rust,compile_fail /// ```rust,compile_fail,edition2021
/// #![deny(dependency_on_unit_never_type_fallback)] /// #![deny(dependency_on_unit_never_type_fallback)]
/// fn main() { /// fn main() {
/// if true { /// if true {

View file

@ -138,18 +138,26 @@ impl<'tcx> Value<TyCtxt<'tcx>> for &[ty::Variance] {
cycle_error: &CycleError, cycle_error: &CycleError,
_guar: ErrorGuaranteed, _guar: ErrorGuaranteed,
) -> Self { ) -> Self {
if let Some(frame) = cycle_error.cycle.get(0) search_for_cycle_permutation(
&& frame.query.dep_kind == dep_kinds::variances_of &cycle_error.cycle,
&& let Some(def_id) = frame.query.def_id |cycle| {
{ if let Some(frame) = cycle.get(0)
let n = tcx.generics_of(def_id).own_params.len(); && frame.query.dep_kind == dep_kinds::variances_of
vec![ty::Bivariant; n].leak() && let Some(def_id) = frame.query.def_id
} else { {
span_bug!( let n = tcx.generics_of(def_id).own_params.len();
cycle_error.usage.as_ref().unwrap().0, ControlFlow::Break(vec![ty::Bivariant; n].leak())
"only `variances_of` returns `&[ty::Variance]`" } else {
); ControlFlow::Continue(())
} }
},
|| {
span_bug!(
cycle_error.usage.as_ref().unwrap().0,
"only `variances_of` returns `&[ty::Variance]`"
)
},
)
} }
} }

View file

@ -444,21 +444,15 @@ impl<'a> LintExtractor<'a> {
fs::write(&tempfile, source) fs::write(&tempfile, source)
.map_err(|e| format!("failed to write {}: {}", tempfile.display(), e))?; .map_err(|e| format!("failed to write {}: {}", tempfile.display(), e))?;
let mut cmd = Command::new(self.rustc_path); let mut cmd = Command::new(self.rustc_path);
if options.contains(&"edition2024") { let edition = options
cmd.arg("--edition=2024"); .iter()
cmd.arg("-Zunstable-options"); .filter_map(|opt| opt.strip_prefix("edition"))
} else if options.contains(&"edition2021") { .next()
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 {
// defaults to latest edition // 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("--error-format=json");
cmd.arg("--target").arg(self.rustc_target); cmd.arg("--target").arg(self.rustc_target);
if let Some(target_linker) = self.rustc_linker { if let Some(target_linker) = self.rustc_linker {

View file

@ -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); }

View file

@ -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
tests/pretty/if-else.pp Normal file
View file

@ -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); }

65
tests/pretty/if-else.rs Normal file
View file

@ -0,0 +1,65 @@
//@ 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);
}

View file

@ -0,0 +1,17 @@
#![feature(prelude_import)]
#![no_std]
//@ pretty-mode:expanded
//@ pp-exact:never-pattern.pp
//@ only-x86_64
#![allow(incomplete_features)]
#![feature(never_patterns)]
#![feature(never_type)]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
fn f(x: Result<u32, !>) { _ = match x { Ok(x) => x, Err(!) , }; }
fn main() {}

View file

@ -0,0 +1,16 @@
//@ pretty-mode:expanded
//@ pp-exact:never-pattern.pp
//@ only-x86_64
#![allow(incomplete_features)]
#![feature(never_patterns)]
#![feature(never_type)]
fn f(x: Result<u32, !>) {
_ = match x {
Ok(x) => x,
Err(!),
};
}
fn main() {}

View file

@ -18,18 +18,18 @@ fn arbitrary_consuming_method_for_demonstration_purposes() {
let mut __capture0 = ::core::asserting::Capture::new(); let mut __capture0 = ::core::asserting::Capture::new();
let __local_bind0 = &elem; let __local_bind0 = &elem;
if ::core::intrinsics::unlikely(!(*{ if ::core::intrinsics::unlikely(!(*{
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
__local_bind0 __local_bind0
} as usize)) { } as usize)) {
{ {
::std::rt::panic_fmt(format_args!("Assertion failed: elem as usize\nWith captures:\n elem = {0:?}\n", ::std::rt::panic_fmt(format_args!("Assertion failed: elem as usize\nWith captures:\n elem = {0:?}\n",
__capture0)); __capture0));
}
} }
}
}; };
} }
fn addr_of() { fn addr_of() {
@ -40,12 +40,12 @@ fn addr_of() {
let mut __capture0 = ::core::asserting::Capture::new(); let mut __capture0 = ::core::asserting::Capture::new();
let __local_bind0 = &elem; let __local_bind0 = &elem;
if ::core::intrinsics::unlikely(!&*__local_bind0) { if ::core::intrinsics::unlikely(!&*__local_bind0) {
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
{ {
::std::rt::panic_fmt(format_args!("Assertion failed: &elem\nWith captures:\n elem = {0:?}\n", ::std::rt::panic_fmt(format_args!("Assertion failed: &elem\nWith captures:\n elem = {0:?}\n",
__capture0)); __capture0));
}
} }
}
}; };
} }
fn binary() { fn binary() {
@ -56,12 +56,12 @@ fn binary() {
let mut __capture0 = ::core::asserting::Capture::new(); let mut __capture0 = ::core::asserting::Capture::new();
let __local_bind0 = &elem; let __local_bind0 = &elem;
if ::core::intrinsics::unlikely(!(*__local_bind0 == 1)) { if ::core::intrinsics::unlikely(!(*__local_bind0 == 1)) {
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
{ {
::std::rt::panic_fmt(format_args!("Assertion failed: elem == 1\nWith captures:\n elem = {0:?}\n", ::std::rt::panic_fmt(format_args!("Assertion failed: elem == 1\nWith captures:\n elem = {0:?}\n",
__capture0)); __capture0));
}
} }
}
}; };
{ {
#[allow(unused_imports)] #[allow(unused_imports)]
@ -69,12 +69,12 @@ fn binary() {
let mut __capture0 = ::core::asserting::Capture::new(); let mut __capture0 = ::core::asserting::Capture::new();
let __local_bind0 = &elem; let __local_bind0 = &elem;
if ::core::intrinsics::unlikely(!(*__local_bind0 >= 1)) { if ::core::intrinsics::unlikely(!(*__local_bind0 >= 1)) {
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
{ {
::std::rt::panic_fmt(format_args!("Assertion failed: elem >= 1\nWith captures:\n elem = {0:?}\n", ::std::rt::panic_fmt(format_args!("Assertion failed: elem >= 1\nWith captures:\n elem = {0:?}\n",
__capture0)); __capture0));
}
} }
}
}; };
{ {
#[allow(unused_imports)] #[allow(unused_imports)]
@ -82,12 +82,12 @@ fn binary() {
let mut __capture0 = ::core::asserting::Capture::new(); let mut __capture0 = ::core::asserting::Capture::new();
let __local_bind0 = &elem; let __local_bind0 = &elem;
if ::core::intrinsics::unlikely(!(*__local_bind0 > 0)) { if ::core::intrinsics::unlikely(!(*__local_bind0 > 0)) {
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
{ {
::std::rt::panic_fmt(format_args!("Assertion failed: elem > 0\nWith captures:\n elem = {0:?}\n", ::std::rt::panic_fmt(format_args!("Assertion failed: elem > 0\nWith captures:\n elem = {0:?}\n",
__capture0)); __capture0));
}
} }
}
}; };
{ {
#[allow(unused_imports)] #[allow(unused_imports)]
@ -95,12 +95,12 @@ fn binary() {
let mut __capture0 = ::core::asserting::Capture::new(); let mut __capture0 = ::core::asserting::Capture::new();
let __local_bind0 = &elem; let __local_bind0 = &elem;
if ::core::intrinsics::unlikely(!(*__local_bind0 < 3)) { if ::core::intrinsics::unlikely(!(*__local_bind0 < 3)) {
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
{ {
::std::rt::panic_fmt(format_args!("Assertion failed: elem < 3\nWith captures:\n elem = {0:?}\n", ::std::rt::panic_fmt(format_args!("Assertion failed: elem < 3\nWith captures:\n elem = {0:?}\n",
__capture0)); __capture0));
}
} }
}
}; };
{ {
#[allow(unused_imports)] #[allow(unused_imports)]
@ -108,12 +108,12 @@ fn binary() {
let mut __capture0 = ::core::asserting::Capture::new(); let mut __capture0 = ::core::asserting::Capture::new();
let __local_bind0 = &elem; let __local_bind0 = &elem;
if ::core::intrinsics::unlikely(!(*__local_bind0 <= 3)) { if ::core::intrinsics::unlikely(!(*__local_bind0 <= 3)) {
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
{ {
::std::rt::panic_fmt(format_args!("Assertion failed: elem <= 3\nWith captures:\n elem = {0:?}\n", ::std::rt::panic_fmt(format_args!("Assertion failed: elem <= 3\nWith captures:\n elem = {0:?}\n",
__capture0)); __capture0));
}
} }
}
}; };
{ {
#[allow(unused_imports)] #[allow(unused_imports)]
@ -121,12 +121,12 @@ fn binary() {
let mut __capture0 = ::core::asserting::Capture::new(); let mut __capture0 = ::core::asserting::Capture::new();
let __local_bind0 = &elem; let __local_bind0 = &elem;
if ::core::intrinsics::unlikely(!(*__local_bind0 != 3)) { if ::core::intrinsics::unlikely(!(*__local_bind0 != 3)) {
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
{ {
::std::rt::panic_fmt(format_args!("Assertion failed: elem != 3\nWith captures:\n elem = {0:?}\n", ::std::rt::panic_fmt(format_args!("Assertion failed: elem != 3\nWith captures:\n elem = {0:?}\n",
__capture0)); __capture0));
}
} }
}
}; };
} }
fn unary() { fn unary() {
@ -137,12 +137,12 @@ fn unary() {
let mut __capture0 = ::core::asserting::Capture::new(); let mut __capture0 = ::core::asserting::Capture::new();
let __local_bind0 = &elem; let __local_bind0 = &elem;
if ::core::intrinsics::unlikely(!**__local_bind0) { if ::core::intrinsics::unlikely(!**__local_bind0) {
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
{ {
::std::rt::panic_fmt(format_args!("Assertion failed: *elem\nWith captures:\n elem = {0:?}\n", ::std::rt::panic_fmt(format_args!("Assertion failed: *elem\nWith captures:\n elem = {0:?}\n",
__capture0)); __capture0));
}
} }
}
}; };
} }
fn main() {} fn main() {}

View file

@ -8,10 +8,10 @@ extern crate std;
//@ edition:2015 //@ edition:2015
fn main() ({ fn main() ({
(if (true as bool) (if (true as bool) {
({ } as } else if (let Some(a) =
()) else if (let Some(a) = ((Some as
((Some as fn(i32) -> Option<i32> {Option::<i32>::Some})((3 as i32)) as
fn(i32) -> Option<i32> {Option::<i32>::Some})((3 as i32)) as Option<i32>) as bool) {
Option<i32>) as bool) ({ } as ()) as ()) } as ())
} as ()) } as ())

View file

@ -32,12 +32,12 @@ fn main() {
let mut iter = let mut iter =
"\"world\"".parse::<crate::TokenStream>().unwrap().into_iter(); "\"world\"".parse::<crate::TokenStream>().unwrap().into_iter();
if let (Some(crate::TokenTree::Literal(mut lit)), None) = if let (Some(crate::TokenTree::Literal(mut lit)), None) =
(iter.next(), iter.next()) { (iter.next(), iter.next()) {
lit.set_span(crate::Span::recover_proc_macro_span(2)); lit.set_span(crate::Span::recover_proc_macro_span(2));
lit lit
} else { } else {
::core::panicking::panic("internal error: entered unreachable code") ::core::panicking::panic("internal error: entered unreachable code")
} }
}), &mut ts); }), &mut ts);
crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new(';', crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new(';',
crate::Spacing::Alone)), &mut ts); crate::Spacing::Alone)), &mut ts);
@ -51,12 +51,12 @@ fn main() {
let mut iter = let mut iter =
"r#\"raw\"literal\"#".parse::<crate::TokenStream>().unwrap().into_iter(); "r#\"raw\"literal\"#".parse::<crate::TokenStream>().unwrap().into_iter();
if let (Some(crate::TokenTree::Literal(mut lit)), None) = if let (Some(crate::TokenTree::Literal(mut lit)), None) =
(iter.next(), iter.next()) { (iter.next(), iter.next()) {
lit.set_span(crate::Span::recover_proc_macro_span(5)); lit.set_span(crate::Span::recover_proc_macro_span(5));
lit lit
} else { } else {
::core::panicking::panic("internal error: entered unreachable code") ::core::panicking::panic("internal error: entered unreachable code")
} }
}), &mut ts); }), &mut ts);
crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new(';', crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new(';',
crate::Spacing::Alone)), &mut ts); crate::Spacing::Alone)), &mut ts);