Skip to content

Commit da2752e

Browse files
committed
check accessibility before suggesting wrapping expressions
1 parent 7163e7f commit da2752e

File tree

3 files changed

+93
-8
lines changed

3 files changed

+93
-8
lines changed

compiler/rustc_typeck/src/check/demand.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc_span::{BytePos, Span};
1919
use super::method::probe;
2020

2121
use std::iter;
22-
use std::ops::Bound;
2322

2423
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2524
pub fn emit_coerce_suggestions(
@@ -349,13 +348,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
349348
}
350349
}
351350

352-
// Avoid suggesting wrapping in `NonZeroU64` and alike
353-
if self.tcx.layout_scalar_valid_range(expected_adt.did())
354-
!= (Bound::Unbounded, Bound::Unbounded)
355-
{
356-
return;
357-
}
358-
359351
let compatible_variants: Vec<String> = expected_adt
360352
.variants()
361353
.iter()
@@ -364,6 +356,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
364356
})
365357
.filter_map(|variant| {
366358
let sole_field = &variant.fields[0];
359+
360+
if !sole_field.did.is_local()
361+
&& !sole_field.vis.is_accessible_from(
362+
self.tcx.parent_module(expr.hir_id).to_def_id(),
363+
self.tcx,
364+
)
365+
{
366+
return None;
367+
}
368+
367369
let sole_field_ty = sole_field.ty(self.tcx, substs);
368370
if self.can_coerce(expr_ty, sole_field_ty) {
369371
let variant_path =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
mod inner {
2+
pub struct Wrapper<T>(T);
3+
}
4+
5+
fn needs_wrapper(t: inner::Wrapper<i32>) {}
6+
fn needs_wrapping(t: std::num::Wrapping<i32>) {}
7+
fn needs_ready(t: std::future::Ready<i32>) {}
8+
9+
fn main() {
10+
// Suggest wrapping expression because type is local
11+
// and its privacy can be easily changed
12+
needs_wrapper(0);
13+
//~^ ERROR mismatched types
14+
//~| HELP try wrapping the expression in `inner::Wrapper`
15+
16+
// Suggest wrapping expression because field is accessible
17+
needs_wrapping(0);
18+
//~^ ERROR mismatched types
19+
//~| HELP try wrapping the expression in `std::num::Wrapping`
20+
21+
// Do not suggest wrapping expression
22+
needs_ready(Some(0));
23+
//~^ ERROR mismatched types
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/wrap-suggestion-privacy.rs:12:19
3+
|
4+
LL | needs_wrapper(0);
5+
| ------------- ^ expected struct `Wrapper`, found integer
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: expected struct `Wrapper<i32>`
10+
found type `{integer}`
11+
note: function defined here
12+
--> $DIR/wrap-suggestion-privacy.rs:5:4
13+
|
14+
LL | fn needs_wrapper(t: inner::Wrapper<i32>) {}
15+
| ^^^^^^^^^^^^^ ----------------------
16+
help: try wrapping the expression in `inner::Wrapper`
17+
|
18+
LL | needs_wrapper(inner::Wrapper(0));
19+
| +++++++++++++++ +
20+
21+
error[E0308]: mismatched types
22+
--> $DIR/wrap-suggestion-privacy.rs:17:20
23+
|
24+
LL | needs_wrapping(0);
25+
| -------------- ^ expected struct `Wrapping`, found integer
26+
| |
27+
| arguments to this function are incorrect
28+
|
29+
= note: expected struct `Wrapping<i32>`
30+
found type `{integer}`
31+
note: function defined here
32+
--> $DIR/wrap-suggestion-privacy.rs:6:4
33+
|
34+
LL | fn needs_wrapping(t: std::num::Wrapping<i32>) {}
35+
| ^^^^^^^^^^^^^^ --------------------------
36+
help: try wrapping the expression in `std::num::Wrapping`
37+
|
38+
LL | needs_wrapping(std::num::Wrapping(0));
39+
| +++++++++++++++++++ +
40+
41+
error[E0308]: mismatched types
42+
--> $DIR/wrap-suggestion-privacy.rs:22:17
43+
|
44+
LL | needs_ready(Some(0));
45+
| ----------- ^^^^^^^ expected struct `std::future::Ready`, found enum `Option`
46+
| |
47+
| arguments to this function are incorrect
48+
|
49+
= note: expected struct `std::future::Ready<i32>`
50+
found enum `Option<{integer}>`
51+
note: function defined here
52+
--> $DIR/wrap-suggestion-privacy.rs:7:4
53+
|
54+
LL | fn needs_ready(t: std::future::Ready<i32>) {}
55+
| ^^^^^^^^^^^ --------------------------
56+
57+
error: aborting due to 3 previous errors
58+
59+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)