Skip to content

Commit f3ac328

Browse files
author
mejrs
committed
Address feedback
1 parent c4c9415 commit f3ac328

File tree

10 files changed

+113
-30
lines changed

10 files changed

+113
-30
lines changed

compiler/rustc_hir_analysis/src/check/method/suggest.rs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14281428
item_name,
14291429
field_ty,
14301430
call_expr,
1431-
ProbeScope::AllTraits,
1431+
ProbeScope::TraitsInScope,
14321432
)
14331433
.ok()
14341434
.map(|pick| (variant, field, pick))
@@ -1500,59 +1500,88 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15001500
item_name,
15011501
ty,
15021502
call_expr,
1503-
ProbeScope::AllTraits,
1503+
ProbeScope::TraitsInScope,
15041504
) else { return; };
15051505

15061506
let name = self.ty_to_value_string(actual);
15071507
let inner_id = kind.did();
1508+
let mutable = if let Some(AutorefOrPtrAdjustment::Autoref { mutbl, .. }) =
1509+
pick.autoref_or_ptr_adjustment
1510+
{
1511+
Some(mutbl)
1512+
} else {
1513+
None
1514+
};
15081515

15091516
if tcx.is_diagnostic_item(sym::LocalKey, inner_id) {
1510-
err.help("use `with` or `try_with` to access the contents of threadlocals");
1517+
err.help("use `with` or `try_with` to access thread local storage");
15111518
} else if Some(kind.did()) == tcx.lang_items().maybe_uninit() {
15121519
err.help(format!(
15131520
"if this `{name}` has been initialized, \
15141521
use one of the `assume_init` methods to access the inner value"
15151522
));
15161523
} else if tcx.is_diagnostic_item(sym::RefCell, inner_id) {
1517-
match pick.autoref_or_ptr_adjustment {
1518-
Some(AutorefOrPtrAdjustment::Autoref {
1519-
mutbl: Mutability::Not, ..
1520-
}) => {
1524+
match mutable {
1525+
Some(Mutability::Not) => {
15211526
err.span_suggestion_verbose(
15221527
expr.span.shrink_to_hi(),
15231528
format!(
1524-
"use `.borrow()` to borrow the {ty}, \
1525-
panicking if any outstanding mutable borrows exist."
1529+
"use `.borrow()` to borrow the `{ty}`, \
1530+
panicking if any outstanding mutable borrows exist."
15261531
),
15271532
".borrow()",
15281533
Applicability::MaybeIncorrect,
15291534
);
15301535
}
1531-
Some(AutorefOrPtrAdjustment::Autoref {
1532-
mutbl: Mutability::Mut, ..
1533-
}) => {
1536+
Some(Mutability::Mut) => {
15341537
err.span_suggestion_verbose(
15351538
expr.span.shrink_to_hi(),
15361539
format!(
1537-
"use `.borrow_mut()` to mutably borrow the {ty}, \
1538-
panicking if any outstanding borrows exist."
1540+
"use `.borrow_mut()` to mutably borrow the `{ty}`, \
1541+
panicking if any outstanding borrows exist."
15391542
),
15401543
".borrow_mut()",
15411544
Applicability::MaybeIncorrect,
15421545
);
15431546
}
1544-
_ => return,
1547+
None => return,
15451548
}
15461549
} else if tcx.is_diagnostic_item(sym::Mutex, inner_id) {
15471550
err.span_suggestion_verbose(
15481551
expr.span.shrink_to_hi(),
15491552
format!(
1550-
"use `.lock()` to borrow the {ty}, \
1553+
"use `.lock()` to borrow the `{ty}`, \
15511554
blocking the current thread until it can be acquired"
15521555
),
15531556
".lock().unwrap()",
15541557
Applicability::MaybeIncorrect,
15551558
);
1559+
} else if tcx.is_diagnostic_item(sym::RwLock, inner_id) {
1560+
match mutable {
1561+
Some(Mutability::Not) => {
1562+
err.span_suggestion_verbose(
1563+
expr.span.shrink_to_hi(),
1564+
format!(
1565+
"use `.read()` to borrow the `{ty}`, \
1566+
blocking the current thread until it can be acquired"
1567+
),
1568+
".read().unwrap()",
1569+
Applicability::MaybeIncorrect,
1570+
);
1571+
}
1572+
Some(Mutability::Mut) => {
1573+
err.span_suggestion_verbose(
1574+
expr.span.shrink_to_hi(),
1575+
format!(
1576+
"use `.write()` to mutably borrow the `{ty}`, \
1577+
blocking the current thread until it can be acquired"
1578+
),
1579+
".write().unwrap()",
1580+
Applicability::MaybeIncorrect,
1581+
);
1582+
}
1583+
None => return,
1584+
}
15561585
} else {
15571586
return;
15581587
};

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ symbols! {
276276
Rust,
277277
RustcDecodable,
278278
RustcEncodable,
279+
RwLock,
279280
RwLockReadGuard,
280281
RwLockWriteGuard,
281282
Send,

library/core/src/cell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ impl<T, const N: usize> Cell<[T; N]> {
614614
/// A mutable memory location with dynamically checked borrow rules
615615
///
616616
/// See the [module-level documentation](self) for more.
617-
#[rustc_diagnostic_item = "RefCell"]
617+
#[cfg_attr(not(test), rustc_diagnostic_item = "RefCell")]
618618
#[stable(feature = "rust1", since = "1.0.0")]
619619
pub struct RefCell<T: ?Sized> {
620620
borrow: Cell<BorrowFlag>,

library/std/src/sync/rwlock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ use crate::sys_common::rwlock as sys;
7676
///
7777
/// [`Mutex`]: super::Mutex
7878
#[stable(feature = "rust1", since = "1.0.0")]
79+
#[cfg_attr(not(test), rustc_diagnostic_item = "RwLock")]
7980
pub struct RwLock<T: ?Sized> {
8081
inner: sys::MovableRwLock,
8182
poison: poison::Flag,

library/std/src/thread/local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ use crate::fmt;
9595
/// [loader lock]: https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices
9696
/// [`JoinHandle::join`]: crate::thread::JoinHandle::join
9797
/// [`with`]: LocalKey::with
98-
#[rustc_diagnostic_item = "LocalKey"]
98+
#[cfg_attr(not(test), rustc_diagnostic_item = "LocalKey")]
9999
#[stable(feature = "rust1", since = "1.0.0")]
100100
pub struct LocalKey<T: 'static> {
101101
// This outer `LocalKey<T>` type is what's going to be stored in statics,

src/test/ui/suggestions/inner_type.fixed

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,25 @@ fn main() {
1616

1717
other_item.borrow().method();
1818
//~^ ERROR no method named `method` found for struct `RefCell` in the current scope [E0599]
19-
//~| HELP use `.borrow()` to borrow the Struct<u32>, panicking if any outstanding mutable borrows exist.
19+
//~| HELP use `.borrow()` to borrow the `Struct<u32>`, panicking if any outstanding mutable borrows exist.
2020

2121
other_item.borrow_mut().some_mutable_method();
2222
//~^ ERROR no method named `some_mutable_method` found for struct `RefCell` in the current scope [E0599]
23-
//~| HELP use `.borrow_mut()` to mutably borrow the Struct<u32>, panicking if any outstanding borrows exist.
23+
//~| HELP use `.borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any outstanding borrows exist.
2424

2525
let another_item = std::sync::Mutex::new(Struct { p: 42_u32 });
2626

2727
another_item.lock().unwrap().method();
2828
//~^ ERROR no method named `method` found for struct `Mutex` in the current scope [E0599]
29-
//~| HELP use `.lock()` to borrow the Struct<u32>, blocking the current thread until it can be acquired
29+
//~| HELP use `.lock()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
30+
31+
let another_item = std::sync::RwLock::new(Struct { p: 42_u32 });
32+
33+
another_item.read().unwrap().method();
34+
//~^ ERROR no method named `method` found for struct `RwLock` in the current scope [E0599]
35+
//~| HELP use `.read()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
36+
37+
another_item.write().unwrap().some_mutable_method();
38+
//~^ ERROR no method named `some_mutable_method` found for struct `RwLock` in the current scope [E0599]
39+
//~| HELP use `.write()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
3040
}

src/test/ui/suggestions/inner_type.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,25 @@ fn main() {
1616

1717
other_item.method();
1818
//~^ ERROR no method named `method` found for struct `RefCell` in the current scope [E0599]
19-
//~| HELP use `.borrow()` to borrow the Struct<u32>, panicking if any outstanding mutable borrows exist.
19+
//~| HELP use `.borrow()` to borrow the `Struct<u32>`, panicking if any outstanding mutable borrows exist.
2020

2121
other_item.some_mutable_method();
2222
//~^ ERROR no method named `some_mutable_method` found for struct `RefCell` in the current scope [E0599]
23-
//~| HELP use `.borrow_mut()` to mutably borrow the Struct<u32>, panicking if any outstanding borrows exist.
23+
//~| HELP use `.borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any outstanding borrows exist.
2424

2525
let another_item = std::sync::Mutex::new(Struct { p: 42_u32 });
2626

2727
another_item.method();
2828
//~^ ERROR no method named `method` found for struct `Mutex` in the current scope [E0599]
29-
//~| HELP use `.lock()` to borrow the Struct<u32>, blocking the current thread until it can be acquired
29+
//~| HELP use `.lock()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
30+
31+
let another_item = std::sync::RwLock::new(Struct { p: 42_u32 });
32+
33+
another_item.method();
34+
//~^ ERROR no method named `method` found for struct `RwLock` in the current scope [E0599]
35+
//~| HELP use `.read()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
36+
37+
another_item.some_mutable_method();
38+
//~^ ERROR no method named `some_mutable_method` found for struct `RwLock` in the current scope [E0599]
39+
//~| HELP use `.write()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
3040
}

src/test/ui/suggestions/inner_type.stderr

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: the method `method` exists on the type `Struct<u32>`
99
|
1010
LL | pub fn method(&self) {}
1111
| ^^^^^^^^^^^^^^^^^^^^
12-
help: use `.borrow()` to borrow the Struct<u32>, panicking if any outstanding mutable borrows exist.
12+
help: use `.borrow()` to borrow the `Struct<u32>`, panicking if any outstanding mutable borrows exist.
1313
|
1414
LL | other_item.borrow().method();
1515
| +++++++++
@@ -25,7 +25,7 @@ note: the method `some_mutable_method` exists on the type `Struct<u32>`
2525
|
2626
LL | pub fn some_mutable_method(&mut self) {}
2727
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28-
help: use `.borrow_mut()` to mutably borrow the Struct<u32>, panicking if any outstanding borrows exist.
28+
help: use `.borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any outstanding borrows exist.
2929
|
3030
LL | other_item.borrow_mut().some_mutable_method();
3131
| +++++++++++++
@@ -41,11 +41,43 @@ note: the method `method` exists on the type `Struct<u32>`
4141
|
4242
LL | pub fn method(&self) {}
4343
| ^^^^^^^^^^^^^^^^^^^^
44-
help: use `.lock()` to borrow the Struct<u32>, blocking the current thread until it can be acquired
44+
help: use `.lock()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
4545
|
4646
LL | another_item.lock().unwrap().method();
4747
| ++++++++++++++++
4848

49-
error: aborting due to 3 previous errors
49+
error[E0599]: no method named `method` found for struct `RwLock` in the current scope
50+
--> $DIR/inner_type.rs:33:18
51+
|
52+
LL | another_item.method();
53+
| ^^^^^^ method not found in `RwLock<Struct<u32>>`
54+
|
55+
note: the method `method` exists on the type `Struct<u32>`
56+
--> $DIR/inner_type.rs:9:5
57+
|
58+
LL | pub fn method(&self) {}
59+
| ^^^^^^^^^^^^^^^^^^^^
60+
help: use `.read()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
61+
|
62+
LL | another_item.read().unwrap().method();
63+
| ++++++++++++++++
64+
65+
error[E0599]: no method named `some_mutable_method` found for struct `RwLock` in the current scope
66+
--> $DIR/inner_type.rs:37:18
67+
|
68+
LL | another_item.some_mutable_method();
69+
| ^^^^^^^^^^^^^^^^^^^ method not found in `RwLock<Struct<u32>>`
70+
|
71+
note: the method `some_mutable_method` exists on the type `Struct<u32>`
72+
--> $DIR/inner_type.rs:11:5
73+
|
74+
LL | pub fn some_mutable_method(&mut self) {}
75+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
76+
help: use `.write()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
77+
|
78+
LL | another_item.write().unwrap().some_mutable_method();
79+
| +++++++++++++++++
80+
81+
error: aborting due to 5 previous errors
5082

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

src/test/ui/suggestions/inner_type2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ thread_local! {
1717
fn main() {
1818
STRUCT.method();
1919
//~^ ERROR no method named `method` found for struct `LocalKey` in the current scope [E0599]
20-
//~| HELP use `with` or `try_with` to access the contents of threadlocals
20+
//~| HELP use `with` or `try_with` to access thread local storage
2121

2222
let item = std::mem::MaybeUninit::new(Struct { p: 42_u32 });
2323
item.method();

src/test/ui/suggestions/inner_type2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0599]: no method named `method` found for struct `LocalKey` in the curren
44
LL | STRUCT.method();
55
| ^^^^^^ method not found in `LocalKey<Struct<u32>>`
66
|
7-
= help: use `with` or `try_with` to access the contents of threadlocals
7+
= help: use `with` or `try_with` to access thread local storage
88
note: the method `method` exists on the type `Struct<u32>`
99
--> $DIR/inner_type2.rs:6:5
1010
|

0 commit comments

Comments
 (0)