Skip to content

Commit ab16eeb

Browse files
authored
Rollup merge of rust-lang#133843 - estebank:empty-semi-sugg, r=jieyouxu
Do not emit empty suggestion The `println!();` statement's span doesn't include the `;`, and the modified suggestions where trying to get the `;` by getting the differenece between the statement's and the expression's spans, which was an empty suggestion. Fix rust-lang#133833, fix rust-lang#133834.
2 parents d692b1f + 1b449e1 commit ab16eeb

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -3394,7 +3394,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33943394
let Some(ty) = self.node_ty_opt(tail_expr.hir_id) else {
33953395
return;
33963396
};
3397-
if self.can_eq(self.param_env, expected_ty, ty) {
3397+
if self.can_eq(self.param_env, expected_ty, ty)
3398+
// FIXME: this happens with macro calls. Need to figure out why the stmt
3399+
// `println!();` doesn't include the `;` in its `Span`. (#133845)
3400+
// We filter these out to avoid ICEs with debug assertions on caused by
3401+
// empty suggestions.
3402+
&& stmt.span.hi() != tail_expr.span.hi()
3403+
{
33983404
err.span_suggestion_short(
33993405
stmt.span.with_lo(tail_expr.span.hi()),
34003406
"remove this semicolon",

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+6
Original file line numberDiff line numberDiff line change
@@ -3838,6 +3838,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
38383838
&& self.predicate_must_hold_modulo_regions(&Obligation::misc(
38393839
tcx, expr.span, body_id, param_env, pred,
38403840
))
3841+
&& expr.span.hi() != rcvr.span.hi()
38413842
{
38423843
err.span_suggestion_verbose(
38433844
expr.span.with_lo(rcvr.span.hi()),
@@ -4115,6 +4116,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
41154116
// the expected is a projection that we need to resolve.
41164117
// && let Some(tail_ty) = typeck_results.expr_ty_opt(expr)
41174118
&& expected_found.found.is_unit()
4119+
// FIXME: this happens with macro calls. Need to figure out why the stmt
4120+
// `println!();` doesn't include the `;` in its `Span`. (#133845)
4121+
// We filter these out to avoid ICEs with debug assertions on caused by
4122+
// empty suggestions.
4123+
&& expr.span.hi() != stmt.span.hi()
41184124
{
41194125
err.span_suggestion_verbose(
41204126
expr.span.shrink_to_hi().with_hi(stmt.span.hi()),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//! Regression test for suggestions that were fired on empty spans
2+
//! involving macro-call statements. For some reason the semicolon
3+
//! is not included in the overall span of the macro-call statement.
4+
//!
5+
//! Issue 1: <https://github.com/rust-lang/rust/issues/133833>.
6+
//! Issue 2: <https://github.com/rust-lang/rust/issues/133834>.
7+
//! See also: <https://github.com/rust-lang/rust/issues/133845>.
8+
9+
fn foo() -> String {
10+
let mut list = {
11+
println!();
12+
};
13+
list //~ ERROR mismatched types
14+
}
15+
16+
fn bar() {
17+
String::new()
18+
.chars()
19+
.filter(|x| !x.is_whitespace())
20+
.map(|x| {
21+
println!("Child spawned with the size: {}", x);
22+
})
23+
.collect::<String>(); //~ ERROR E0277
24+
}
25+
26+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/semi-suggestion-when-stmt-and-expr-span-equal.rs:13:5
3+
|
4+
LL | fn foo() -> String {
5+
| ------ expected `String` because of return type
6+
LL | let mut list = {
7+
| ____________________-
8+
LL | | println!();
9+
LL | | };
10+
| |_____- this block is missing a tail expression
11+
LL | list
12+
| ^^^^ expected `String`, found `()`
13+
14+
error[E0277]: a value of type `String` cannot be built from an iterator over elements of type `()`
15+
--> $DIR/semi-suggestion-when-stmt-and-expr-span-equal.rs:23:20
16+
|
17+
LL | .collect::<String>();
18+
| ------- ^^^^^^ value of type `String` cannot be built from `std::iter::Iterator<Item=()>`
19+
| |
20+
| required by a bound introduced by this call
21+
|
22+
= help: the trait `FromIterator<()>` is not implemented for `String`
23+
= help: the following other types implement trait `FromIterator<A>`:
24+
`String` implements `FromIterator<&char>`
25+
`String` implements `FromIterator<&str>`
26+
`String` implements `FromIterator<Box<str, A>>`
27+
`String` implements `FromIterator<Cow<'_, str>>`
28+
`String` implements `FromIterator<String>`
29+
`String` implements `FromIterator<char>`
30+
note: the method call chain might not have had the expected associated types
31+
--> $DIR/semi-suggestion-when-stmt-and-expr-span-equal.rs:20:10
32+
|
33+
LL | String::new()
34+
| ------------- this expression has type `String`
35+
LL | .chars()
36+
| ------- `Iterator::Item` is `char` here
37+
LL | .filter(|x| !x.is_whitespace())
38+
| ------------------------------ `Iterator::Item` remains `char` here
39+
LL | .map(|x| {
40+
| __________^
41+
LL | | println!("Child spawned with the size: {}", x);
42+
LL | | })
43+
| |__________^ `Iterator::Item` changed to `()` here
44+
note: required by a bound in `collect`
45+
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
46+
47+
error: aborting due to 2 previous errors
48+
49+
Some errors have detailed explanations: E0277, E0308.
50+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)