Skip to content

Commit 613a3b6

Browse files
authored
Rollup merge of #146428 - jieyouxu:revert-assert-desugaring, r=estebank,jackh726
Revert `assert!` desugaring changes (#122661) Reverts #122661 to prevent #145770 slipping into beta. cc `@estebank` (FYI) ### Review remarks - Commit 1 is the MCVE reported in #145770 added as a regression test `tests/ui/macros/assert-desugaring-145770.rs`. Against `master`, this test fails. - Commit 2 reverts #122661 (with a merge conflict fixed). `tests/ui/macros/assert-desugaring-145770.rs` now passes.
2 parents 5258dfc + b38a86f commit 613a3b6

File tree

16 files changed

+74
-135
lines changed

16 files changed

+74
-135
lines changed

compiler/rustc_builtin_macros/src/assert.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
mod context;
22

3-
use rustc_ast::token::{self, Delimiter};
3+
use rustc_ast::token::Delimiter;
44
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
5-
use rustc_ast::{DelimArgs, Expr, ExprKind, MacCall, Path, PathSegment};
5+
use rustc_ast::{DelimArgs, Expr, ExprKind, MacCall, Path, PathSegment, UnOp, token};
66
use rustc_ast_pretty::pprust;
77
use rustc_errors::PResult;
88
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
@@ -29,7 +29,7 @@ pub(crate) fn expand_assert<'cx>(
2929

3030
// `core::panic` and `std::panic` are different macros, so we use call-site
3131
// context to pick up whichever is currently in scope.
32-
let call_site_span = cx.with_call_site_ctxt(cond_expr.span);
32+
let call_site_span = cx.with_call_site_ctxt(span);
3333

3434
let panic_path = || {
3535
if use_panic_2021(span) {
@@ -63,7 +63,7 @@ pub(crate) fn expand_assert<'cx>(
6363
}),
6464
})),
6565
);
66-
assert_cond_check(cx, call_site_span, cond_expr, then)
66+
expr_if_not(cx, call_site_span, cond_expr, then, None)
6767
}
6868
// If `generic_assert` is enabled, generates rich captured outputs
6969
//
@@ -88,33 +88,26 @@ pub(crate) fn expand_assert<'cx>(
8888
)),
8989
)],
9090
);
91-
assert_cond_check(cx, call_site_span, cond_expr, then)
91+
expr_if_not(cx, call_site_span, cond_expr, then, None)
9292
};
9393

9494
ExpandResult::Ready(MacEager::expr(expr))
9595
}
9696

97-
/// `assert!($cond_expr, $custom_message)`
9897
struct Assert {
9998
cond_expr: Box<Expr>,
10099
custom_message: Option<TokenStream>,
101100
}
102101

103-
/// `match <cond> { true => {} _ => <then> }`
104-
fn assert_cond_check(cx: &ExtCtxt<'_>, span: Span, cond: Box<Expr>, then: Box<Expr>) -> Box<Expr> {
105-
// Instead of expanding to `if !<cond> { <then> }`, we expand to
106-
// `match <cond> { true => {} _ => <then> }`.
107-
// This allows us to always complain about mismatched types instead of "cannot apply unary
108-
// operator `!` to type `X`" when passing an invalid `<cond>`, while also allowing `<cond>` to
109-
// be `&true`.
110-
let els = cx.expr_block(cx.block(span, thin_vec![]));
111-
let mut arms = thin_vec![];
112-
arms.push(cx.arm(span, cx.pat_lit(span, cx.expr_bool(span, true)), els));
113-
arms.push(cx.arm(span, cx.pat_wild(span), then));
114-
115-
// We wrap the `match` in a statement to limit the length of any borrows introduced in the
116-
// condition.
117-
cx.expr_block(cx.block(span, [cx.stmt_expr(cx.expr_match(span, cond, arms))].into()))
102+
// if !{ ... } { ... } else { ... }
103+
fn expr_if_not(
104+
cx: &ExtCtxt<'_>,
105+
span: Span,
106+
cond: Box<Expr>,
107+
then: Box<Expr>,
108+
els: Option<Box<Expr>>,
109+
) -> Box<Expr> {
110+
cx.expr_if(span, cx.expr(span, ExprKind::Unary(UnOp::Not, cond)), then, els)
118111
}
119112

120113
fn parse_assert<'a>(cx: &ExtCtxt<'a>, sp: Span, stream: TokenStream) -> PResult<'a, Assert> {

compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,18 +1618,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
16181618
{
16191619
let e = self.tcx.erase_and_anonymize_regions(e);
16201620
let f = self.tcx.erase_and_anonymize_regions(f);
1621-
let mut expected = with_forced_trimmed_paths!(e.sort_string(self.tcx));
1622-
let mut found = with_forced_trimmed_paths!(f.sort_string(self.tcx));
1623-
if let ObligationCauseCode::Pattern { span, .. } = cause.code()
1624-
&& let Some(span) = span
1625-
&& !span.from_expansion()
1626-
&& cause.span.from_expansion()
1627-
{
1628-
// When the type error comes from a macro like `assert!()`, and we are pointing at
1629-
// code the user wrote the cause and effect are reversed as the expected value is
1630-
// what the macro expanded to.
1631-
(found, expected) = (expected, found);
1632-
}
1621+
let expected = with_forced_trimmed_paths!(e.sort_string(self.tcx));
1622+
let found = with_forced_trimmed_paths!(f.sort_string(self.tcx));
16331623
if expected == found {
16341624
label_or_note(span, terr.to_string(self.tcx));
16351625
} else {
@@ -2152,9 +2142,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21522142
) -> Option<(DiagStyledString, DiagStyledString)> {
21532143
match values {
21542144
ValuePairs::Regions(exp_found) => self.expected_found_str(exp_found),
2155-
ValuePairs::Terms(exp_found) => {
2156-
self.expected_found_str_term(cause, exp_found, long_ty_path)
2157-
}
2145+
ValuePairs::Terms(exp_found) => self.expected_found_str_term(exp_found, long_ty_path),
21582146
ValuePairs::Aliases(exp_found) => self.expected_found_str(exp_found),
21592147
ValuePairs::ExistentialTraitRef(exp_found) => self.expected_found_str(exp_found),
21602148
ValuePairs::ExistentialProjection(exp_found) => self.expected_found_str(exp_found),
@@ -2193,35 +2181,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21932181

21942182
fn expected_found_str_term(
21952183
&self,
2196-
cause: &ObligationCause<'tcx>,
21972184
exp_found: ty::error::ExpectedFound<ty::Term<'tcx>>,
21982185
long_ty_path: &mut Option<PathBuf>,
21992186
) -> Option<(DiagStyledString, DiagStyledString)> {
22002187
let exp_found = self.resolve_vars_if_possible(exp_found);
22012188
if exp_found.references_error() {
22022189
return None;
22032190
}
2204-
let (mut expected, mut found) = (exp_found.expected, exp_found.found);
2205-
2206-
if let ObligationCauseCode::Pattern { span, .. } = cause.code()
2207-
&& let Some(span) = span
2208-
&& !span.from_expansion()
2209-
&& cause.span.from_expansion()
2210-
{
2211-
// When the type error comes from a macro like `assert!()`, and we are pointing at
2212-
// code the user wrote, the cause and effect are reversed as the expected value is
2213-
// what the macro expanded to. So if the user provided a `Type` when the macro is
2214-
// written in such a way that a `bool` was expected, we want to print:
2215-
// = note: expected `bool`
2216-
// found `Type`"
2217-
// but as far as the compiler is concerned, after expansion what was expected was `Type`
2218-
// = note: expected `Type`
2219-
// found `bool`"
2220-
// so we reverse them here to match user expectation.
2221-
(expected, found) = (found, expected);
2222-
}
22232191

2224-
Some(match (expected.kind(), found.kind()) {
2192+
Some(match (exp_found.expected.kind(), exp_found.found.kind()) {
22252193
(ty::TermKind::Ty(expected), ty::TermKind::Ty(found)) => {
22262194
let (mut exp, mut fnd) = self.cmp(expected, found);
22272195
// Use the terminal width as the basis to determine when to compress the printed

src/tools/clippy/clippy_lints/src/missing_asserts_for_indexing.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_ast::{BinOpKind, LitKind, RangeLimits};
1111
use rustc_data_structures::packed::Pu128;
1212
use rustc_data_structures::unhash::UnindexMap;
1313
use rustc_errors::{Applicability, Diag};
14-
use rustc_hir::{Body, Expr, ExprKind};
14+
use rustc_hir::{Block, Body, Expr, ExprKind, UnOp};
1515
use rustc_lint::{LateContext, LateLintPass};
1616
use rustc_session::declare_lint_pass;
1717
use rustc_span::source_map::Spanned;
@@ -135,12 +135,12 @@ fn assert_len_expr<'hir>(
135135
cx: &LateContext<'_>,
136136
expr: &'hir Expr<'hir>,
137137
) -> Option<(LengthComparison, usize, &'hir Expr<'hir>)> {
138-
let (cmp, asserted_len, slice_len) = if let Some(
139-
higher::IfLetOrMatch::Match(cond, [_, then], _)
140-
) = higher::IfLetOrMatch::parse(cx, expr)
141-
&& let ExprKind::Binary(bin_op, left, right) = &cond.kind
138+
let (cmp, asserted_len, slice_len) = if let Some(higher::If { cond, then, .. }) = higher::If::hir(expr)
139+
&& let ExprKind::Unary(UnOp::Not, condition) = &cond.kind
140+
&& let ExprKind::Binary(bin_op, left, right) = &condition.kind
142141
// check if `then` block has a never type expression
143-
&& cx.typeck_results().expr_ty(then.body).is_never()
142+
&& let ExprKind::Block(Block { expr: Some(then_expr), .. }, _) = then.kind
143+
&& cx.typeck_results().expr_ty(then_expr).is_never()
144144
{
145145
len_comparison(bin_op.node, left, right)?
146146
} else if let Some((macro_call, bin_op)) = first_node_macro_backtrace(cx, expr).find_map(|macro_call| {

src/tools/clippy/tests/ui/const_is_empty.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ fn issue_13106() {
196196

197197
const {
198198
assert!(EMPTY_STR.is_empty());
199-
//~^ const_is_empty
200199
}
201200

202201
const {

src/tools/clippy/tests/ui/const_is_empty.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,10 @@ LL | let _ = val.is_empty();
158158
| ^^^^^^^^^^^^^^
159159

160160
error: this expression always evaluates to true
161-
--> tests/ui/const_is_empty.rs:198:17
162-
|
163-
LL | assert!(EMPTY_STR.is_empty());
164-
| ^^^^^^^^^^^^^^^^^^^^
165-
166-
error: this expression always evaluates to true
167-
--> tests/ui/const_is_empty.rs:203:9
161+
--> tests/ui/const_is_empty.rs:202:9
168162
|
169163
LL | EMPTY_STR.is_empty();
170164
| ^^^^^^^^^^^^^^^^^^^^
171165

172-
error: aborting due to 28 previous errors
166+
error: aborting due to 27 previous errors
173167

src/tools/clippy/tests/ui/incompatible_msrv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![warn(clippy::incompatible_msrv)]
22
#![feature(custom_inner_attributes)]
3-
#![allow(stable_features, clippy::diverging_sub_expression)]
3+
#![allow(stable_features)]
44
#![feature(strict_provenance)] // For use in test
55
#![clippy::msrv = "1.3.0"]
66

tests/ui/codemap_tests/issue-28308.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11
fn main() {
2-
assert!("foo"); //~ ERROR mismatched types
3-
//~^ NOTE expected `bool`, found `str`
4-
//~| NOTE in this expansion of assert!
5-
let x = Some(&1);
6-
assert!(x); //~ ERROR mismatched types
7-
//~^ NOTE expected `bool`, found `Option<&{integer}>`
8-
//~| NOTE expected enum `bool`
9-
//~| NOTE in this expansion of assert!
10-
//~| NOTE in this expansion of assert!
11-
assert!(x, ""); //~ ERROR mismatched types
12-
//~^ NOTE expected `bool`, found `Option<&{integer}>`
13-
//~| NOTE expected enum `bool`
14-
//~| NOTE in this expansion of assert!
15-
//~| NOTE in this expansion of assert!
2+
assert!("foo");
3+
//~^ ERROR cannot apply unary operator `!`
164
}
Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/issue-28308.rs:2:13
1+
error[E0600]: cannot apply unary operator `!` to type `&'static str`
2+
--> $DIR/issue-28308.rs:2:5
33
|
44
LL | assert!("foo");
5-
| ^^^^^ expected `bool`, found `str`
5+
| ^^^^^^^^^^^^^^ cannot apply unary operator `!`
66

7-
error[E0308]: mismatched types
8-
--> $DIR/issue-28308.rs:6:13
9-
|
10-
LL | assert!(x);
11-
| ^ expected `bool`, found `Option<&{integer}>`
12-
|
13-
= note: expected enum `bool`
14-
found type `Option<&{integer}>`
15-
16-
error[E0308]: mismatched types
17-
--> $DIR/issue-28308.rs:11:13
18-
|
19-
LL | assert!(x, "");
20-
| ^ expected `bool`, found `Option<&{integer}>`
21-
|
22-
= note: expected enum `bool`
23-
found type `Option<&{integer}>`
24-
25-
error: aborting due to 3 previous errors
7+
error: aborting due to 1 previous error
268

27-
For more information about this error, try `rustc --explain E0308`.
9+
For more information about this error, try `rustc --explain E0600`.

tests/ui/consts/control-flow/assert.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0080]: evaluation panicked: assertion failed: false
2-
--> $DIR/assert.rs:5:23
2+
--> $DIR/assert.rs:5:15
33
|
44
LL | const _: () = assert!(false);
5-
| ^^^^^ evaluation of `_` failed here
5+
| ^^^^^^^^^^^^^^ evaluation of `_` failed here
66

77
error: aborting due to 1 previous error
88

tests/ui/generics/post_monomorphization_error_backtrace.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0080]: evaluation panicked: assertion failed: std::mem::size_of::<T>() == 0
2-
--> $DIR/post_monomorphization_error_backtrace.rs:6:31
2+
--> $DIR/post_monomorphization_error_backtrace.rs:6:23
33
|
44
LL | const V: () = assert!(std::mem::size_of::<T>() == 0);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `assert_zst::F::<u32>::V` failed here
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `assert_zst::F::<u32>::V` failed here
66

77
note: erroneous constant encountered
88
--> $DIR/post_monomorphization_error_backtrace.rs:14:5
@@ -17,10 +17,10 @@ LL | assert_zst::<U>()
1717
| ^^^^^^^^^^^^^^^^^
1818

1919
error[E0080]: evaluation panicked: assertion failed: std::mem::size_of::<T>() == 0
20-
--> $DIR/post_monomorphization_error_backtrace.rs:6:31
20+
--> $DIR/post_monomorphization_error_backtrace.rs:6:23
2121
|
2222
LL | const V: () = assert!(std::mem::size_of::<T>() == 0);
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `assert_zst::F::<i32>::V` failed here
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `assert_zst::F::<i32>::V` failed here
2424

2525
note: erroneous constant encountered
2626
--> $DIR/post_monomorphization_error_backtrace.rs:14:5

0 commit comments

Comments
 (0)