Skip to content

Commit 154f5cb

Browse files
committed
add field traits
1 parent 03707aa commit 154f5cb

File tree

9 files changed

+122
-6
lines changed

9 files changed

+122
-6
lines changed

compiler/rustc_hir/src/lang_items.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,14 @@ language_item_table! {
440440

441441
// Reborrowing related lang-items
442442
Reborrow, sym::reborrow, reborrow, Target::Trait, GenericRequirement::Exact(0);
443+
444+
// Experimental lang items for field projections.
445+
Field, sym::Field, field_trait, Target::Trait, GenericRequirement::None;
446+
UnalignedField, sym::UnalignedField, unaligned_field_trait, Target::Trait, GenericRequirement::None;
447+
UnalignedFieldBase, sym::UnalignedFieldBase, unaligned_field_base, Target::AssocTy, GenericRequirement::None;
448+
UnalignedFieldType, sym::UnalignedFieldType, unaligned_field_type, Target::AssocTy, GenericRequirement::None;
449+
UnalignedFieldOFFSET, sym::UnalignedFieldOFFSET, unaligned_field_offset, Target::AssocConst, GenericRequirement::None;
450+
UnalignedFieldOffset, sym::unaligned_field_offset, unaligned_field_offset_getter, Target::Fn, GenericRequirement::Exact(1);
443451
}
444452

445453
/// The requirement imposed on the generics of a lang item

compiler/rustc_middle/src/ty/context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ bidirectional_lang_item_map! {
831831
Destruct,
832832
DiscriminantKind,
833833
Drop,
834+
Field,
834835
Fn,
835836
FnMut,
836837
FnOnce,
@@ -844,6 +845,7 @@ bidirectional_lang_item_map! {
844845
Sized,
845846
TransmuteTrait,
846847
Tuple,
848+
UnalignedField,
847849
Unpin,
848850
Unsize,
849851
// tidy-alphabetical-end
@@ -2579,6 +2581,7 @@ impl<'tcx> TyCtxt<'tcx> {
25792581
fmt,
25802582
self,
25812583
Adt,
2584+
Field,
25822585
Array,
25832586
Slice,
25842587
RawPtr,

compiler/rustc_span/src/symbol.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ symbols! {
234234
Equal,
235235
Err,
236236
Error,
237+
Field,
237238
File,
238239
FileType,
239240
FmtArgumentsNew,
@@ -378,6 +379,10 @@ symbols! {
378379
Ty,
379380
TyCtxt,
380381
TyKind,
382+
UnalignedField,
383+
UnalignedFieldBase,
384+
UnalignedFieldOFFSET,
385+
UnalignedFieldType,
381386
Unknown,
382387
Unsize,
383388
UnsizedConstParamTy,

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,9 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
996996
| LangItem::FnOnce
997997
| LangItem::AsyncFn
998998
| LangItem::AsyncFnMut
999-
| LangItem::AsyncFnOnce,
999+
| LangItem::AsyncFnOnce
1000+
| LangItem::Field
1001+
| LangItem::UnalignedField,
10001002
) => true,
10011003
Some(LangItem::AsyncFnKindHelper) => {
10021004
// FIXME(async_closures): Validity constraints here could be cleaned up.

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
282282
| LangItem::FnPtrTrait
283283
| LangItem::PointeeTrait
284284
| LangItem::Tuple
285-
| LangItem::Unpin,
285+
| LangItem::Unpin
286+
| LangItem::Field
287+
| LangItem::UnalignedField,
286288
) => ty::Binder::dummy(vec![]),
287289
other => bug!("unexpected builtin trait {trait_def:?} ({other:?})"),
288290
};

compiler/rustc_type_ir/src/lang_items.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub enum SolverTraitLangItem {
3636
Destruct,
3737
DiscriminantKind,
3838
Drop,
39+
Field,
3940
Fn,
4041
FnMut,
4142
FnOnce,
@@ -49,6 +50,7 @@ pub enum SolverTraitLangItem {
4950
Sized,
5051
TransmuteTrait,
5152
Tuple,
53+
UnalignedField,
5254
Unpin,
5355
Unsize,
5456
// tidy-alphabetical-end

library/core/src/field.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,33 @@
11
//! Field Reflection
2+
3+
/// Type representing a field of a `struct`.
4+
///
5+
/// # Safety
6+
///
7+
/// Given a valid value of type `Self::Base`, there exists a valid value of type `Self::Type` at
8+
/// byte offset `OFFSET`.
9+
#[lang = "UnalignedField"]
10+
#[unstable(feature = "field_projections", issue = "145383")]
11+
pub unsafe trait UnalignedField: Sized {
12+
/// The type of the base where this field exists in.
13+
#[lang = "UnalignedFieldBase"]
14+
type Base: ?Sized;
15+
16+
/// The type of the field.
17+
#[lang = "UnalignedFieldType"]
18+
type Type: ?Sized;
19+
20+
/// The offset of the field in bytes.
21+
#[lang = "UnalignedFieldOFFSET"]
22+
const OFFSET: usize;
23+
}
24+
25+
/// Type representing an aligned field of a `struct`.
26+
///
27+
/// # Safety
28+
///
29+
/// Given a well-aligned value of type `Self::Base`, the field at `Self::OFFSET` of type
30+
/// `Self::Type` is well-aligned.
31+
#[lang = "Field"]
32+
#[unstable(feature = "field_projections", issue = "145383")]
33+
pub unsafe trait Field: UnalignedField {}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
#![allow(dead_code)]
22

3-
use std::field; //~ ERROR: use of unstable library feature `field_projections`
3+
use std::field::Field; //~ ERROR: use of unstable library feature `field_projections` [E0658]
4+
use std::ptr;
5+
6+
fn project_ref<F: Field>(
7+
//~^ ERROR: use of unstable library feature `field_projections` [E0658]
8+
r: &F::Base, //~ ERROR: use of unstable library feature `field_projections` [E0658]
9+
) -> &F::Type
10+
//~^ ERROR: use of unstable library feature `field_projections` [E0658]
11+
where
12+
F::Type: Sized, //~ ERROR: use of unstable library feature `field_projections` [E0658]
13+
{
14+
unsafe { &*ptr::from_ref(r).byte_add(F::OFFSET).cast() } //~ ERROR: use of unstable library feature `field_projections` [E0658]
15+
}
416

517
fn main() {}
Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,63 @@
11
error[E0658]: use of unstable library feature `field_projections`
22
--> $DIR/feature-gate-field-projections.rs:3:5
33
|
4-
LL | use std::field;
5-
| ^^^^^^^^^^
4+
LL | use std::field::Field;
5+
| ^^^^^^^^^^^^^^^^^
66
|
77
= note: see issue #145383 <https://github.com/rust-lang/rust/issues/145383> for more information
88
= help: add `#![feature(field_projections)]` to the crate attributes to enable
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

11-
error: aborting due to 1 previous error
11+
error[E0658]: use of unstable library feature `field_projections`
12+
--> $DIR/feature-gate-field-projections.rs:6:19
13+
|
14+
LL | fn project_ref<F: Field>(
15+
| ^^^^^
16+
|
17+
= note: see issue #145383 <https://github.com/rust-lang/rust/issues/145383> for more information
18+
= help: add `#![feature(field_projections)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error[E0658]: use of unstable library feature `field_projections`
22+
--> $DIR/feature-gate-field-projections.rs:12:5
23+
|
24+
LL | F::Type: Sized,
25+
| ^^^^^^^
26+
|
27+
= note: see issue #145383 <https://github.com/rust-lang/rust/issues/145383> for more information
28+
= help: add `#![feature(field_projections)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30+
31+
error[E0658]: use of unstable library feature `field_projections`
32+
--> $DIR/feature-gate-field-projections.rs:8:9
33+
|
34+
LL | r: &F::Base,
35+
| ^^^^^^^
36+
|
37+
= note: see issue #145383 <https://github.com/rust-lang/rust/issues/145383> for more information
38+
= help: add `#![feature(field_projections)]` to the crate attributes to enable
39+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
40+
41+
error[E0658]: use of unstable library feature `field_projections`
42+
--> $DIR/feature-gate-field-projections.rs:9:7
43+
|
44+
LL | ) -> &F::Type
45+
| ^^^^^^^
46+
|
47+
= note: see issue #145383 <https://github.com/rust-lang/rust/issues/145383> for more information
48+
= help: add `#![feature(field_projections)]` to the crate attributes to enable
49+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
50+
51+
error[E0658]: use of unstable library feature `field_projections`
52+
--> $DIR/feature-gate-field-projections.rs:14:42
53+
|
54+
LL | unsafe { &*ptr::from_ref(r).byte_add(F::OFFSET).cast() }
55+
| ^^^^^^^^^
56+
|
57+
= note: see issue #145383 <https://github.com/rust-lang/rust/issues/145383> for more information
58+
= help: add `#![feature(field_projections)]` to the crate attributes to enable
59+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
60+
61+
error: aborting due to 6 previous errors
1262

1363
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)