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

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5dd54fd
lint/ctypes: ext. abi fn-ptr in internal abi fn
davidtwco Mar 1, 2023
c1dcf26
abi: avoid ice for non-ffi-safe fn ptrs
davidtwco Mar 1, 2023
05c1e6b
stabilize debugger visualizer attribute
gibbyfree Mar 2, 2023
92ae35f
lint/ctypes: multiple external fn-ptrs in ty
davidtwco Mar 2, 2023
5c34f77
whitespace, delete unstable book page
gibbyfree Mar 2, 2023
3e7ca3e
lint/ctypes: check other types for ext. fn-ptr ty
davidtwco Mar 2, 2023
1a7132d
rustdoc-search: fix incorrect doc comment
notriddle Apr 14, 2023
4c11822
rustdoc: restructure type search engine to pick-and-use IDs
notriddle Apr 15, 2023
1ece1ea
Stablize raw-dylib, link_ordinal and -Cdlltool
dpaoliello Mar 27, 2023
b6f81e0
rustdoc-search: give longer notification for type corrections
notriddle Apr 19, 2023
e0a7462
rustdoc-search: clean up `checkIfInGenerics` call at end of `checkType`
notriddle Apr 20, 2023
7529d87
rustdoc-search: make type name correction choice deterministic
notriddle Apr 20, 2023
395840c
rustdoc-search: use more descriptive "x not found; y instead" message
notriddle Apr 20, 2023
d77f636
libtest: add tests for junit output format
durin42 Apr 21, 2023
610f827
junit: also include per-case stdout in xml
durin42 Apr 21, 2023
d5e7ac5
avoid duplicating TLS state between test std and realstd
RalfJung Apr 28, 2023
58537cd
junit: fix typo in comment and don't include output for passes when n…
durin42 Apr 28, 2023
bc68de9
remove pointless `FIXME` in `bootstrap::download`
onur-ozkan May 1, 2023
b540b5f
Tweak borrow suggestion
compiler-errors Apr 18, 2023
1ca3bdf
Rename some suggestion/note functions
compiler-errors Apr 18, 2023
f0e7af4
Make suggest_deref_or_ref return a multipart suggestion
compiler-errors Apr 18, 2023
b00e5f3
remove bootstrap from tests
gibbyfree Mar 6, 2023
c9653a6
fix stderrs
gibbyfree Mar 6, 2023
1795530
Rollup merge of #108611 - davidtwco:issue-94223-external-abi-fn-ptr-i…
matthiaskrgr May 1, 2023
dcef3b7
Rollup merge of #108668 - gibbyfree:stabilizedebuggervisualizer, r=we…
matthiaskrgr May 1, 2023
ca7717e
Rollup merge of #109677 - dpaoliello:rawdylib, r=michaelwoerister,wes…
matthiaskrgr May 1, 2023
b3261cb
Rollup merge of #110371 - notriddle:notriddle/search-corrections, r=G…
matthiaskrgr May 1, 2023
4af1284
Rollup merge of #110504 - compiler-errors:tweak-borrow-sugg, r=cjgillot
matthiaskrgr May 1, 2023
35bc61e
Rollup merge of #110651 - durin42:xunit-stdout, r=cuviper
matthiaskrgr May 1, 2023
ffd8460
Rollup merge of #110946 - RalfJung:tls-realstd, r=m-ou-se
matthiaskrgr May 1, 2023
5945ab8
Rollup merge of #111069 - ozkanonur:remove-pointless-fixme, r=albertl…
matthiaskrgr May 1, 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 1, 2023
commit b540b5fcd589ef0134a190866f77b16aa8803bb3
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