Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6c97f13
Invalid `?` suggestion on mismatched `Ok(T)`
eopb Oct 20, 2023
24cdb27
let_chainify `suggest_coercing_result_via_try_operator`
eopb Oct 22, 2023
8716890
Enable cg_clif tests for riscv64gc
bjorn3 Oct 21, 2023
c1bfd46
When expecting closure argument but finding block provide suggestion
estebank Oct 23, 2023
f0a2635
Redo `stringify.rs` test.
nnethercote Oct 19, 2023
2e2e780
Augment `stringify.rs` test.
nnethercote Oct 19, 2023
c1800fb
Augment `stringify.rs` test some more.
nnethercote Oct 24, 2023
173dcb2
Touch up syn parsing in symbols macro
dtolnay Oct 25, 2023
ba17934
Represent symbol value as enum to prepare for supporting env vars
dtolnay Oct 25, 2023
95742ff
Add a Parse impl for symbol Value
dtolnay Oct 25, 2023
8dea49a
Delete counter from symbols proc macro in favor of hashmap as source …
dtolnay Oct 25, 2023
726a43c
Move symbols macro map into a struct
dtolnay Oct 25, 2023
65f0253
Support environment variable for interned Symbol value
dtolnay Oct 25, 2023
1078250
Continue generating other symbols if an expr is not supported
dtolnay Oct 25, 2023
5563a9b
Improve span of env-related errors
dtolnay Oct 25, 2023
ac4fa3f
Pre-intern a symbol for env!("CFG_RELEASE")
dtolnay Oct 25, 2023
c1552df
Fix symbols::tests::test_symbols
dtolnay Oct 25, 2023
94ecabf
Add comment to mem::replace to explain why it's not implemented via m…
chfogelman Oct 26, 2023
934cbe4
Rollup merge of #116968 - eopb:116967, r=petrochenkov
matthiaskrgr Oct 26, 2023
596369f
Rollup merge of #117032 - bjorn3:riscv64_enable_cg_clif_tests, r=petr…
matthiaskrgr Oct 26, 2023
d09c988
Rollup merge of #117106 - estebank:issue-27300, r=petrochenkov
matthiaskrgr Oct 26, 2023
a8f7acd
Rollup merge of #117114 - nnethercote:improve-stringify-test, r=petro…
matthiaskrgr Oct 26, 2023
2656c98
Rollup merge of #117188 - dtolnay:symbolenv, r=cjgillot
matthiaskrgr Oct 26, 2023
c0b1c1a
Rollup merge of #117243 - chfogelman:replace-not-swap-comment, r=thomcc
matthiaskrgr Oct 26, 2023
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
Prev Previous commit
Next Next commit
When expecting closure argument but finding block provide suggestion
Detect if there is a potential typo where the `{` meant to open the
closure body was written before the body.

```
error[E0277]: expected a `FnOnce<({integer},)>` closure, found `Option<usize>`
  --> $DIR/ruby_style_closure_successful_parse.rs:3:31
   |
LL |       let p = Some(45).and_then({|x|
   |  ______________________--------_^
   | |                      |
   | |                      required by a bound introduced by this call
LL | |         1 + 1;
LL | |         Some(x * 2)
   | |         ----------- this tail expression is of type `Option<usize>`
LL | |     });
   | |_____^ expected an `FnOnce<({integer},)>` closure, found `Option<usize>`
   |
   = help: the trait `FnOnce<({integer},)>` is not implemented for `Option<usize>`
note: required by a bound in `Option::<T>::and_then`
  --> $SRC_DIR/core/src/option.rs:LL:COL
help: you might have meant to open the closure body instead of placing a closure within a block
   |
LL -     let p = Some(45).and_then({|x|
LL +     let p = Some(45).and_then(|x| {
   |
```

Detect the potential typo where the closure header is missing.

```
error[E0277]: expected a `FnOnce<(&bool,)>` closure, found `bool`
  --> $DIR/block_instead_of_closure_in_arg.rs:3:23
   |
LL |        Some(true).filter({
   |  _________________------_^
   | |                 |
   | |                 required by a bound introduced by this call
LL | |/         if number % 2 == 0 {
LL | ||             number == 0
LL | ||         } else {
LL | ||             number != 0
LL | ||         }
   | ||_________- this tail expression is of type `bool`
LL | |      });
   | |______^ expected an `FnOnce<(&bool,)>` closure, found `bool`
   |
   = help: the trait `for<'a> FnOnce<(&'a bool,)>` is not implemented for `bool`
note: required by a bound in `Option::<T>::filter`
  --> $SRC_DIR/core/src/option.rs:LL:COL
help: you might have meant to create the closure instead of a block
   |
LL |     Some(true).filter(|_| {
   |                       +++
```

Partially address #27300.
  • Loading branch information
estebank committed Oct 23, 2023
commit c1bfd46c7b2b5ec21275fdab2fa931c54f9b1f9c
Original file line number Diff line number Diff line change
Expand Up @@ -3442,11 +3442,11 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
if let Some(Node::Expr(expr)) = hir.find(arg_hir_id)
&& let Some(typeck_results) = &self.typeck_results
{
if let hir::Expr { kind: hir::ExprKind::Block(..), .. } = expr {
let expr = expr.peel_blocks();
let ty =
typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx));
let span = expr.span;
if let hir::Expr { kind: hir::ExprKind::Block(block, _), .. } = expr {
let inner_expr = expr.peel_blocks();
let ty = typeck_results.expr_ty_adjusted_opt(inner_expr)
.unwrap_or(Ty::new_misc_error(tcx));
let span = inner_expr.span;
if Some(span) != err.span.primary_span() {
err.span_label(
span,
Expand All @@ -3457,6 +3457,49 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
format!("this tail expression is of type `{ty}`")
},
);
if let ty::PredicateKind::Clause(clause) = failed_pred.kind().skip_binder()
&& let ty::ClauseKind::Trait(pred) = clause
&& [
tcx.lang_items().fn_once_trait(),
tcx.lang_items().fn_mut_trait(),
tcx.lang_items().fn_trait(),
].contains(&Some(pred.def_id()))
{
if let [stmt, ..] = block.stmts
&& let hir::StmtKind::Semi(value) = stmt.kind
&& let hir::ExprKind::Closure(hir::Closure {
body,
fn_decl_span,
..
}) = value.kind
&& let body = hir.body(*body)
&& !matches!(body.value.kind, hir::ExprKind::Block(..))
{
// Check if the failed predicate was an expectation of a closure type
// and if there might have been a `{ |args|` typo instead of `|args| {`.
err.multipart_suggestion(
"you might have meant to open the closure body instead of placing \
a closure within a block",
vec![
(expr.span.with_hi(value.span.lo()), String::new()),
(fn_decl_span.shrink_to_hi(), " {".to_string()),
],
Applicability::MaybeIncorrect,
);
} else {
// Maybe the bare block was meant to be a closure.
err.span_suggestion_verbose(
expr.span.shrink_to_lo(),
"you might have meant to create the closure instead of a block",
format!(
"|{}| ",
(0..pred.trait_ref.args.len() - 1).map(|_| "_")
.collect::<Vec<_>>()
.join(", ")),
Applicability::MaybeIncorrect,
);
}
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/expr/malformed_closure/block_instead_of_closure_in_arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
let number = 2;
Some(true).filter({ //~ ERROR expected a `FnOnce<(&bool,)>` closure, found `bool`
if number % 2 == 0 {
number == 0
} else {
number != 0
}
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0277]: expected a `FnOnce<(&bool,)>` closure, found `bool`
--> $DIR/block_instead_of_closure_in_arg.rs:3:23
|
LL | Some(true).filter({
| _________________------_^
| | |
| | required by a bound introduced by this call
LL | |/ if number % 2 == 0 {
LL | || number == 0
LL | || } else {
LL | || number != 0
LL | || }
| ||_________- this tail expression is of type `bool`
LL | | });
| |______^ expected an `FnOnce<(&bool,)>` closure, found `bool`
|
= help: the trait `for<'a> FnOnce<(&'a bool,)>` is not implemented for `bool`
note: required by a bound in `Option::<T>::filter`
--> $SRC_DIR/core/src/option.rs:LL:COL
help: you might have meant to create the closure instead of a block
|
LL | Some(true).filter(|_| {
| +++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const x: usize =42;
fn main() {
let p = Some(45).and_then({|x| //~ ERROR expected a `FnOnce<({integer},)>` closure, found `Option<usize>`
1 + 1;
Some(x * 2)
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error[E0277]: expected a `FnOnce<({integer},)>` closure, found `Option<usize>`
--> $DIR/ruby_style_closure_successful_parse.rs:3:31
|
LL | let p = Some(45).and_then({|x|
| ______________________--------_^
| | |
| | required by a bound introduced by this call
LL | | 1 + 1;
LL | | Some(x * 2)
| | ----------- this tail expression is of type `Option<usize>`
LL | | });
| |_____^ expected an `FnOnce<({integer},)>` closure, found `Option<usize>`
|
= help: the trait `FnOnce<({integer},)>` is not implemented for `Option<usize>`
note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL
help: you might have meant to open the closure body instead of placing a closure within a block
|
LL - let p = Some(45).and_then({|x|
LL + let p = Some(45).and_then(|x| {
|

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.