Skip to content

Rollup of 7 pull requests #111380

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

Merged
merged 33 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
cb4f815
Add GNU Property Note
Apr 22, 2023
fd8aa5e
tweak "make mut" spans when assigning to locals
Ezrashaw Apr 19, 2023
9624d2b
tweak "make mut" spans (No. 2)
Ezrashaw Apr 19, 2023
57c6a31
tweak "make mut" spans (No. 3)
Ezrashaw Apr 19, 2023
336a656
tweak "make mut" spans (No. 4)
Ezrashaw Apr 19, 2023
87a1b38
tweak spans for `ref mut` suggestion
Ezrashaw Apr 20, 2023
d2608df
implement review comment
Ezrashaw Apr 21, 2023
3e64e98
fix trait definition spans in "make mut" suggestion
Ezrashaw Apr 21, 2023
bd928a0
Disallow (min) specialization imps with no items
matthewjasper May 5, 2023
fafe9e7
Normalize consistently for specializations
matthewjasper May 5, 2023
68a5bb4
Add GNU Property Note
May 5, 2023
1fc0442
rewriting match on endianness
May 5, 2023
59ecbd2
Add parsing for builtin # in expression and item context
est31 Jan 19, 2023
5eb29c7
Migrate offset_of from a macro to builtin # syntax
est31 Apr 22, 2023
83b4df4
Add feature gate
est31 Apr 29, 2023
37f3e2f
rewriting match on endianness
May 5, 2023
f46eabb
Report nicer lifetime errors for specialization
matthewjasper May 5, 2023
0bb43c6
Suggest let for possible binding with ty
chenyukang May 3, 2023
20e6e6a
cleanup
chenyukang May 3, 2023
a7fc32c
fix ice in suggesting
chenyukang May 8, 2023
a9051d8
Tweak borrow suggestion
compiler-errors Apr 18, 2023
a283f58
Rename some suggestion/note functions
compiler-errors Apr 18, 2023
4731a25
Make suggest_deref_or_ref return a multipart suggestion
compiler-errors Apr 18, 2023
5e94b5f
code refactor and fix wrong suggestion
chenyukang May 8, 2023
bf7e07f
Update books
rustbot May 8, 2023
4d219d0
move sugg to derive session diagnostic
chenyukang May 9, 2023
02a85bd
Rollup merge of #110304 - cchiw:master, r=davidtwco
Dylan-DPC May 9, 2023
2ecc722
Rollup merge of #110504 - compiler-errors:tweak-borrow-sugg, r=cjgillot
Dylan-DPC May 9, 2023
ff30b8c
Rollup merge of #110583 - Ezrashaw:tweak-make-mut-spans, r=estebank
Dylan-DPC May 9, 2023
dbd090c
Rollup merge of #110694 - est31:builtin, r=petrochenkov
Dylan-DPC May 9, 2023
8c51701
Rollup merge of #111120 - chenyukang:yukang-suggest-let, r=Nilstrieb
Dylan-DPC May 9, 2023
f748bb1
Rollup merge of #111252 - matthewjasper:min-spec-improvements, r=comp…
Dylan-DPC May 9, 2023
9d913eb
Rollup merge of #111361 - rustbot:docs-update, r=ehuss
Dylan-DPC May 9, 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
Tweak borrow suggestion
  • Loading branch information
compiler-errors committed May 8, 2023
commit a9051d861ca13a1c16da3cd4248b26d86cf5e1d5
81 changes: 42 additions & 39 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,52 +1334,55 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
));
}

if let Ok(src) = sm.span_to_snippet(sugg_sp) {
let needs_parens = match expr.kind {
// parenthesize if needed (Issue #46756)
hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true,
// parenthesize borrows of range literals (Issue #54505)
_ if is_range_literal(expr) => true,
_ => false,
};

if let Some(sugg) = self.can_use_as_ref(expr) {
return Some((
sugg.0,
sugg.1.to_string(),
sugg.2,
Applicability::MachineApplicable,
false,
false,
));
}

let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!("{ident}: "),
None => String::new(),
};

if let Some(hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Assign(..),
..
})) = self.tcx.hir().find_parent(expr.hir_id)
{
if mutability.is_mut() {
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
return None;
}
}
let needs_parens = match expr.kind {
// parenthesize if needed (Issue #46756)
hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true,
// parenthesize borrows of range literals (Issue #54505)
_ if is_range_literal(expr) => true,
_ => false,
};

let sugg_expr = if needs_parens { format!("({src})") } else { src };
if let Some(sugg) = self.can_use_as_ref(expr) {
return Some((
sp,
format!("consider {}borrowing here", mutability.mutably_str()),
format!("{prefix}{}{sugg_expr}", mutability.ref_prefix_str()),
sugg.0,
sugg.1.to_string(),
sugg.2,
Applicability::MachineApplicable,
false,
false,
));
}

let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!("{ident}: "),
None => String::new(),
};

if let Some(hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Assign(..),
..
})) = self.tcx.hir().find_parent(expr.hir_id)
{
if mutability.is_mut() {
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
return None;
}
}

let (sp, sugg_expr, verbose) = if needs_parens {
let src = sm.span_to_snippet(sugg_sp).ok()?;
(sp, format!("({src})"), false)
} else {
(sp.shrink_to_lo(), "".to_string(), true)
};
return Some((
sp,
format!("consider {}borrowing here", mutability.mutably_str()),
format!("{prefix}{}{sugg_expr}", mutability.ref_prefix_str()),
Applicability::MachineApplicable,
verbose,
false,
));
}
}
(
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/issue-97484.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LL | fn foo(a: &A, d: D, e: &E, g: G) {}
help: consider borrowing here
|
LL | foo(&&A, B, C, D, &E, F, G);
| ~~
| +
help: remove the extra arguments
|
LL - foo(&&A, B, C, D, E, F, G);
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/async-await/issues/issue-102206.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ error[E0308]: mismatched types
--> $DIR/issue-102206.rs:6:27
|
LL | std::mem::size_of_val(foo());
| --------------------- ^^^^^
| | |
| | expected `&_`, found future
| | help: consider borrowing here: `&foo()`
| --------------------- ^^^^^ expected `&_`, found future
| |
| arguments to this function are incorrect
|
note: function defined here
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
help: consider borrowing here
|
LL | std::mem::size_of_val(&foo());
| +

error: aborting due to previous error

Expand Down
11 changes: 7 additions & 4 deletions tests/ui/coercion/coercion-slice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ error[E0308]: mismatched types
--> $DIR/coercion-slice.rs:4:21
|
LL | let _: &[i32] = [0];
| ------ ^^^
| | |
| | expected `&[i32]`, found `[{integer}; 1]`
| | help: consider borrowing here: `&[0]`
| ------ ^^^ expected `&[i32]`, found `[{integer}; 1]`
| |
| expected due to this
|
help: consider borrowing here
|
LL | let _: &[i32] = &[0];
| +

error: aborting due to previous error

Expand Down
20 changes: 12 additions & 8 deletions tests/ui/inference/deref-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,23 @@ error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:40:17
|
LL | let s = S { u };
| ^
| |
| expected `&u32`, found integer
| help: consider borrowing here: `u: &u`
| ^ expected `&u32`, found integer
|
help: consider borrowing here
|
LL | let s = S { u: &u };
| ++++

error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:42:20
|
LL | let s = S { u: u };
| ^
| |
| expected `&u32`, found integer
| help: consider borrowing here: `&u`
| ^ expected `&u32`, found integer
|
help: consider borrowing here
|
LL | let s = S { u: &u };
| +

error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:45:17
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/issues/issue-11374.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ error[E0308]: mismatched types
--> $DIR/issue-11374.rs:26:15
|
LL | c.read_to(v);
| ------- ^
| | |
| | expected `&mut [u8]`, found `Vec<_>`
| | help: consider mutably borrowing here: `&mut v`
| ------- ^ expected `&mut [u8]`, found `Vec<_>`
| |
| arguments to this method are incorrect
|
= note: expected mutable reference `&mut [u8]`
Expand All @@ -15,6 +13,10 @@ note: method defined here
|
LL | pub fn read_to(&mut self, vec: &mut [u8]) {
| ^^^^^^^ --------------
help: consider mutably borrowing here
|
LL | c.read_to(&mut v);
| ++++

error: aborting due to previous error

Expand Down
11 changes: 7 additions & 4 deletions tests/ui/issues/issue-17033.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ error[E0308]: mismatched types
--> $DIR/issue-17033.rs:2:10
|
LL | (*p)(())
| ---- ^^
| | |
| | expected `&mut ()`, found `()`
| | help: consider mutably borrowing here: `&mut ()`
| ---- ^^ expected `&mut ()`, found `()`
| |
| arguments to this function are incorrect
|
help: consider mutably borrowing here
|
LL | (*p)(&mut ())
| ++++

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-18819.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ LL | fn print_x(_: &dyn Foo<Item=bool>, extra: &str) {
help: consider borrowing here
|
LL | print_x(&X);
| ~~
| +
help: provide the argument
|
LL | print_x(/* &dyn Foo<Item = bool> */, /* &str */);
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/issues/issue-46302.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ error[E0308]: mismatched types
--> $DIR/issue-46302.rs:3:27
|
LL | let u: &str = if true { s[..2] } else { s };
| ^^^^^^
| |
| expected `&str`, found `str`
| help: consider borrowing here: `&s[..2]`
| ^^^^^^ expected `&str`, found `str`
|
help: consider borrowing here
|
LL | let u: &str = if true { &s[..2] } else { s };
| +

error: aborting due to previous error

Expand Down
10 changes: 6 additions & 4 deletions tests/ui/issues/issue-61106.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ error[E0308]: mismatched types
--> $DIR/issue-61106.rs:3:9
|
LL | foo(x.clone());
| --- ^^^^^^^^^
| | |
| | expected `&str`, found `String`
| | help: consider borrowing here: `&x`
| --- ^^^^^^^^^ expected `&str`, found `String`
| |
| arguments to this function are incorrect
|
note: function defined here
--> $DIR/issue-61106.rs:6:4
|
LL | fn foo(_: &str) {}
| ^^^ -------
help: consider borrowing here
|
LL | foo(&x.clone());
| +

error: aborting due to previous error

Expand Down
10 changes: 6 additions & 4 deletions tests/ui/methods/method-self-arg-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ error[E0308]: mismatched types
--> $DIR/method-self-arg-1.rs:11:14
|
LL | Foo::bar(x);
| -------- ^
| | |
| | expected `&Foo`, found `Foo`
| | help: consider borrowing here: `&x`
| -------- ^ expected `&Foo`, found `Foo`
| |
| arguments to this function are incorrect
|
note: method defined here
--> $DIR/method-self-arg-1.rs:6:8
|
LL | fn bar(&self) {}
| ^^^ -----
help: consider borrowing here
|
LL | Foo::bar(&x);
| +

error[E0308]: mismatched types
--> $DIR/method-self-arg-1.rs:13:14
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/mismatched_types/dont-point-return-on-E0308.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ error[E0308]: mismatched types
--> $DIR/dont-point-return-on-E0308.rs:11:11
|
LL | f(());
| - ^^
| | |
| | expected `&()`, found `()`
| | help: consider borrowing here: `&()`
| - ^^ expected `&()`, found `()`
| |
| arguments to this function are incorrect
|
note: function defined here
--> $DIR/dont-point-return-on-E0308.rs:3:10
|
LL | async fn f(_: &()) {}
| ^ ------
help: consider borrowing here
|
LL | f(&());
| +

error: aborting due to previous error

Expand Down
10 changes: 6 additions & 4 deletions tests/ui/mut/mut-cross-borrowing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ error[E0308]: mismatched types
--> $DIR/mut-cross-borrowing.rs:7:7
|
LL | f(x)
| - ^
| | |
| | expected `&mut isize`, found `Box<{integer}>`
| | help: consider mutably borrowing here: `&mut x`
| - ^ expected `&mut isize`, found `Box<{integer}>`
| |
| arguments to this function are incorrect
|
= note: expected mutable reference `&mut isize`
Expand All @@ -15,6 +13,10 @@ note: function defined here
|
LL | fn f(_: &mut isize) {}
| ^ -------------
help: consider mutably borrowing here
|
LL | f(&mut x)
| ++++

error: aborting due to previous error

Expand Down
Loading