Skip to content

Rollup of 9 pull requests #98472

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
00da0e7
make const_err show up in future breakage reports
RalfJung Jun 4, 2022
4a4d877
bless remaining tests
RalfJung Jun 4, 2022
ab3a2a0
Unify copying data from enclave to userspace
raoulstrackx Mar 29, 2022
531752f
Mitigate MMIO stale data vulnerabilities
raoulstrackx Mar 28, 2022
6f7d193
Ensure userspace allocation is 8-byte aligned
raoulstrackx Mar 23, 2022
a27aace
Test `copy_to_userspace` function
raoulstrackx Mar 22, 2022
edb6c4b
Add a test for issue #33172
rylev May 16, 2022
f30c76a
Turn off cdb test for now, link to issue
rylev Jun 20, 2022
3ea686f
Turn CDB test back on and all clarifying test
rylev Jun 20, 2022
e5402e4
Fix linux tests
rylev Jun 21, 2022
1a25ac9
Add comment about issue caused with multiple statics
rylev Jun 21, 2022
6a6910e
Address reviewer comments
raoulstrackx Jun 22, 2022
d23eea5
Add tracking issues to `--extern` option docs.
ehuss Jun 22, 2022
cc4f804
Move help popup into a pocket menu as well
GuillaumeGomez Jun 20, 2022
3eb9e1a
Add/update GUI tests for help pocket menu
GuillaumeGomez Jun 20, 2022
e4b2b41
Merge all popover hide functions into one
GuillaumeGomez Jun 22, 2022
23d325e
Update FIXME comment
rylev Jun 23, 2022
3c7f1f1
Suggest defining variable as mutable on `&mut _` type mismatch in pats
WaffleLapkin Jun 23, 2022
4c4fb71
add test
b-naber Jun 23, 2022
2e3221a
use correct substs in enum discriminant hack
b-naber Jun 23, 2022
38814fc
small refactor
b-naber Jun 24, 2022
f39c0d6
address review
b-naber Jun 24, 2022
ada2acc
Set relocation_model to Pic on emscripten target
hoodmane Jun 15, 2022
bf48b62
fmt
b-naber Jun 24, 2022
e25129b
take advantage of a labelled block
WaffleLapkin Jun 24, 2022
1dfb53b
improve wording of a suggestion
WaffleLapkin Jun 24, 2022
c06d8f9
Fix trait object reborrow suggestion
compiler-errors Jun 20, 2022
25fe474
Note concrete type being coerced into object
compiler-errors Jun 20, 2022
459b151
Rollup merge of #97085 - rylev:test-issue-33172, r=wesleywiser
compiler-errors Jun 25, 2022
7cf4f09
Rollup merge of #97743 - RalfJung:const-err-future-breakage, r=estebank
compiler-errors Jun 25, 2022
11cb0bb
Rollup merge of #98126 - fortanix:raoul/mitigate_stale_data_vulnerabi…
compiler-errors Jun 25, 2022
0bfffb7
Rollup merge of #98149 - hoodmane:emscripten-pic, r=petrochenkov
compiler-errors Jun 25, 2022
be2ef3e
Rollup merge of #98277 - compiler-errors:issue-93596, r=estebank
compiler-errors Jun 25, 2022
1c2e6b6
Rollup merge of #98297 - GuillaumeGomez:help-pocket-menu, r=notriddle
compiler-errors Jun 25, 2022
a6f8881
Rollup merge of #98401 - ehuss:extern-tracking, r=Dylan-DPC
compiler-errors Jun 25, 2022
6030d20
Rollup merge of #98429 - b-naber:use-correct-substs-discriminant-cast…
compiler-errors Jun 25, 2022
bdd6a25
Rollup merge of #98431 - WaffleLapkin:mut_pat_suggestions, r=compiler…
compiler-errors Jun 25, 2022
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
Suggest defining variable as mutable on &mut _ type mismatch in pats
  • Loading branch information
WaffleLapkin committed Jun 23, 2022
commit 3c7f1f16016d8a19a549f2c34a05cfdf8f79968b
51 changes: 51 additions & 0 deletions compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,46 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ast::Mutability::Not => "",
};

let mut_var_suggestion = 'block: {
if !matches!(mutbl, ast::Mutability::Mut) {
break 'block None;
}

let ident_kind = match binding_parent {
hir::Node::Param(_) => Some("parameter"),
hir::Node::Local(_) => Some("variable"),
hir::Node::Arm(_) => Some("binding"),

// Provide diagnostics only if the parent pattern is struct-like,
// i.e. where `mut binding` makes sense
hir::Node::Pat(Pat { kind, .. }) => match kind {
PatKind::Struct(..)
| PatKind::TupleStruct(..)
| PatKind::Or(..)
| PatKind::Tuple(..)
| PatKind::Slice(..) => Some("binding"),

PatKind::Wild
| PatKind::Binding(..)
| PatKind::Path(..)
| PatKind::Box(..)
| PatKind::Ref(..)
| PatKind::Lit(..)
| PatKind::Range(..) => None,
},

// Don't provide suggestions in other cases
_ => None,
};

ident_kind.map(|thing| (
pat.span,
format!("to declare a mutable {thing} use `mut variable_name`"),
format!("mut {binding}"),
))

};

match binding_parent {
// Check that there is explicit type (ie this is not a closure param with inferred type)
// so we don't suggest moving something to the type that does not exist
Expand All @@ -675,6 +715,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
],
Applicability::MachineApplicable
);

if let Some((sp, msg, sugg)) = mut_var_suggestion {
err.span_note(sp, format!("{msg}: `{sugg}`"));
}
}
hir::Node::Param(_) | hir::Node::Arm(_) | hir::Node::Pat(_) => {
// rely on match ergonomics or it might be nested `&&pat`
Expand All @@ -684,6 +728,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"",
Applicability::MaybeIncorrect,
);

if let Some((sp, msg, sugg)) = mut_var_suggestion {
err.span_note(sp, format!("{msg}: `{sugg}`"));
}
}
_ if let Some((sp, msg, sugg)) = mut_var_suggestion => {
err.span_suggestion(sp, msg, sugg, Applicability::MachineApplicable);
}
_ => {} // don't provide suggestions in other cases #55175
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/mismatched_types/ref-pat-suggestions.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,17 @@ fn main() {
let _ = |&mut _a: &mut u32| (); //~ ERROR mismatched types
let _ = |&_a: &u32| (); //~ ERROR mismatched types
let _ = |&mut _a: &mut u32| (); //~ ERROR mismatched types

#[allow(unused_mut)]
{
struct S(u8);

let mut _a = 0; //~ ERROR mismatched types
let S(_b) = S(0); //~ ERROR mismatched types
let (_c,) = (0,); //~ ERROR mismatched types

match 0 {
_d => {} //~ ERROR mismatched types
}
}
}
13 changes: 13 additions & 0 deletions src/test/ui/mismatched_types/ref-pat-suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,17 @@ fn main() {
let _ = |&mut &_a: &mut u32| (); //~ ERROR mismatched types
let _ = |&&mut _a: &u32| (); //~ ERROR mismatched types
let _ = |&mut &mut _a: &mut u32| (); //~ ERROR mismatched types

#[allow(unused_mut)]
{
struct S(u8);

let &mut _a = 0; //~ ERROR mismatched types
let S(&mut _b) = S(0); //~ ERROR mismatched types
let (&mut _c,) = (0,); //~ ERROR mismatched types

match 0 {
&mut _d => {} //~ ERROR mismatched types
}
}
}
92 changes: 91 additions & 1 deletion src/test/ui/mismatched_types/ref-pat-suggestions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ LL | fn _f1(&mut _a: u32) {}
|
= note: expected type `u32`
found mutable reference `&mut _`
note: to declare a mutable parameter use `mut variable_name`: `mut _a`
--> $DIR/ref-pat-suggestions.rs:4:8
|
LL | fn _f1(&mut _a: u32) {}
| ^^^^^^^
help: to take parameter `_a` by reference, move `&mut` to the type
|
LL - fn _f1(&mut _a: u32) {}
Expand Down Expand Up @@ -122,6 +127,11 @@ LL | let _: fn(u32) = |&mut _a| ();
|
= note: expected type `u32`
found mutable reference `&mut _`
note: to declare a mutable parameter use `mut variable_name`: `mut _a`
--> $DIR/ref-pat-suggestions.rs:12:23
|
LL | let _: fn(u32) = |&mut _a| ();
| ^^^^^^^
help: consider removing `&mut` from the pattern
|
LL - let _: fn(u32) = |&mut _a| ();
Expand Down Expand Up @@ -222,6 +232,11 @@ LL | let _ = |&mut _a: u32| ();
|
= note: expected type `u32`
found mutable reference `&mut _`
note: to declare a mutable parameter use `mut variable_name`: `mut _a`
--> $DIR/ref-pat-suggestions.rs:19:14
|
LL | let _ = |&mut _a: u32| ();
| ^^^^^^^
help: to take parameter `_a` by reference, move `&mut` to the type
|
LL - let _ = |&mut _a: u32| ();
Expand Down Expand Up @@ -292,6 +307,81 @@ LL - let _ = |&mut &mut _a: &mut u32| ();
LL + let _ = |&mut _a: &mut u32| ();
|

error: aborting due to 18 previous errors
error[E0308]: mismatched types
--> $DIR/ref-pat-suggestions.rs:29:13
|
LL | let &mut _a = 0;
| ^^^^^^^ - this expression has type `{integer}`
| |
| expected integer, found `&mut _`
| help: to declare a mutable variable use `mut variable_name`: `mut _a`
|
= note: expected type `{integer}`
found mutable reference `&mut _`

error[E0308]: mismatched types
--> $DIR/ref-pat-suggestions.rs:30:15
|
LL | let S(&mut _b) = S(0);
| ^^^^^^^ ---- this expression has type `S`
| |
| expected `u8`, found `&mut _`
|
= note: expected type `u8`
found mutable reference `&mut _`
note: to declare a mutable binding use `mut variable_name`: `mut _b`
--> $DIR/ref-pat-suggestions.rs:30:15
|
LL | let S(&mut _b) = S(0);
| ^^^^^^^
help: consider removing `&mut` from the pattern
|
LL - let S(&mut _b) = S(0);
LL + let S(_b) = S(0);
|

error[E0308]: mismatched types
--> $DIR/ref-pat-suggestions.rs:31:14
|
LL | let (&mut _c,) = (0,);
| ^^^^^^^ ---- this expression has type `({integer},)`
| |
| expected integer, found `&mut _`
|
= note: expected type `{integer}`
found mutable reference `&mut _`
note: to declare a mutable binding use `mut variable_name`: `mut _c`
--> $DIR/ref-pat-suggestions.rs:31:14
|
LL | let (&mut _c,) = (0,);
| ^^^^^^^
help: consider removing `&mut` from the pattern
|
LL - let (&mut _c,) = (0,);
LL + let (_c,) = (0,);
|

error[E0308]: mismatched types
--> $DIR/ref-pat-suggestions.rs:34:13
|
LL | match 0 {
| - this expression has type `{integer}`
LL | &mut _d => {}
| ^^^^^^^ expected integer, found `&mut _`
|
= note: expected type `{integer}`
found mutable reference `&mut _`
note: to declare a mutable binding use `mut variable_name`: `mut _d`
--> $DIR/ref-pat-suggestions.rs:34:13
|
LL | &mut _d => {}
| ^^^^^^^
help: consider removing `&mut` from the pattern
|
LL - &mut _d => {}
LL + _d => {}
|

error: aborting due to 22 previous errors

For more information about this error, try `rustc --explain E0308`.
5 changes: 5 additions & 0 deletions src/test/ui/pattern/for-loop-bad-item.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ LL | for ((_, _), (&mut c, _)) in &mut map {
|
= note: expected type `char`
found mutable reference `&mut _`
note: to declare a mutable binding use `mut variable_name`: `mut c`
--> $DIR/for-loop-bad-item.rs:7:19
|
LL | for ((_, _), (&mut c, _)) in &mut map {
| ^^^^^^
help: consider removing `&mut` from the pattern
|
LL - for ((_, _), (&mut c, _)) in &mut map {
Expand Down