Skip to content

Commit 4f0462e

Browse files
committed
check for auto impls on field representing types
1 parent e6a0420 commit 4f0462e

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@ pub(super) fn check_item<'tcx>(
259259
.with_span_label(sp, "auto trait")
260260
.emit());
261261
}
262+
if is_auto && let rustc_hir::TyKind::FieldOf(..) = impl_.self_ty.kind {
263+
res = res.and(Err(tcx
264+
.dcx()
265+
.struct_span_err(
266+
item.span,
267+
"impls of auto traits for field representing types not supported",
268+
)
269+
.with_span_label(impl_.self_ty.span, "field representing type")
270+
.with_span_label(of_trait.trait_ref.path.span, "auto trait")
271+
.emit()));
272+
}
262273
match header.polarity {
263274
ty::ImplPolarity::Positive => {
264275
res = res.and(check_impl(tcx, item, impl_));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: impls of auto traits for field representing types not supported
2+
--> $DIR/send.rs:13:1
3+
|
4+
LL | unsafe impl Send for field_of!(Bar, bar_field) {}
5+
| ^^^^^^^^^^^^----^^^^^-------------------------^^^
6+
| | |
7+
| | field representing type
8+
| auto trait
9+
10+
error[E0277]: `field_of!(Foo, field)` cannot be sent between threads safely
11+
--> $DIR/send.rs:20:15
12+
|
13+
LL | is_send::<field_of!(Foo, field)>();
14+
| ^^^^^^^^^^^^^^^^^^^^^ `field_of!(Foo, field)` cannot be sent between threads safely
15+
|
16+
= help: the trait `Send` is not implemented for `field_of!(Foo, field)`
17+
= help: the trait `Send` is implemented for `field_of!(Bar, bar_field)`
18+
note: required by a bound in `is_send`
19+
--> $DIR/send.rs:16:15
20+
|
21+
LL | fn is_send<T: Send>() {}
22+
| ^^^^ required by this bound in `is_send`
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0277`.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: impls of auto traits for field representing types not supported
2+
--> $DIR/send.rs:13:1
3+
|
4+
LL | unsafe impl Send for field_of!(Bar, bar_field) {}
5+
| ^^^^^^^^^^^^----^^^^^-------------------------^^^
6+
| | |
7+
| | field representing type
8+
| auto trait
9+
10+
error[E0277]: `field_of!(Foo, field)` cannot be sent between threads safely
11+
--> $DIR/send.rs:20:15
12+
|
13+
LL | is_send::<field_of!(Foo, field)>();
14+
| ^^^^^^^^^^^^^^^^^^^^^ `field_of!(Foo, field)` cannot be sent between threads safely
15+
|
16+
= help: the trait `Send` is not implemented for `field_of!(Foo, field)`
17+
= help: the trait `Send` is implemented for `field_of!(Bar, bar_field)`
18+
note: required by a bound in `is_send`
19+
--> $DIR/send.rs:16:15
20+
|
21+
LL | fn is_send<T: Send>() {}
22+
| ^^^^ required by this bound in `is_send`
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0277`.

tests/ui/field_projections/send.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//@ revisions: old next
22
//@ [next] compile-flags: -Znext-solver
3-
//@ run-pass
43
#![feature(field_projections)]
54
#![allow(incomplete_features)]
65
use std::field::field_of;
@@ -12,10 +11,12 @@ struct Bar {
1211
bar_field: u32,
1312
}
1413
unsafe impl Send for field_of!(Bar, bar_field) {}
14+
//~^ ERROR: impls of auto traits for field representing types not supported
1515

1616
fn is_send<T: Send>() {}
1717

1818
fn main() {
1919
is_send::<field_of!(Bar, bar_field)>();
2020
is_send::<field_of!(Foo, field)>();
21+
//~^ ERROR: `field_of!(Foo, field)` cannot be sent between threads safely [E0277]
2122
}

0 commit comments

Comments
 (0)