Skip to content

Commit 78b2768

Browse files
committed
Auto merge of #137944 - davidtwco:sized-hierarchy, r=oli-obk
Sized Hierarchy: Part I This patch implements the non-const parts of rust-lang/rfcs#3729. It introduces two new traits to the standard library, `MetaSized` and `PointeeSized`. See the RFC for the rationale behind these traits and to discuss whether this change makes sense in the abstract. These traits are unstable (as is their constness), so users cannot refer to them without opting-in to `feature(sized_hierarchy)`. These traits are not behind `cfg`s as this would make implementation unfeasible, there would simply be too many `cfg`s required to add the necessary bounds everywhere. So, like `Sized`, these traits are automatically implemented by the compiler. RFC 3729 describes changes which are necessary to preserve backwards compatibility given the introduction of these traits, which are implemented and as follows: - `?Sized` is rewritten as `MetaSized` - `MetaSized` is added as a default supertrait for all traits w/out an explicit sizedness supertrait already. There are no edition migrations implemented in this, as these are primarily required for the constness parts of the RFC and prior to stabilisation of this (and so will come in follow-up PRs alongside the const parts). All diagnostic output should remain the same (showing `?Sized` even if the compiler sees `MetaSized`) unless the `sized_hierarchy` feature is enabled. Due to the use of unstable extern types in the standard library and rustc, some bounds in both projects have had to be relaxed already - this is unfortunate but unavoidable so that these extern types can continue to be used where they were before. Performing these relaxations in the standard library and rustc are desirable longer-term anyway, but some bounds are not as relaxed as they ideally would be due to the inability to relax `Deref::Target` (this will be investigated separately). It is hoped that this is implemented such that it could be merged and these traits could exist "under the hood" without that being observable to the user (other than in any performance impact this has on the compiler, etc). Some details might leak through due to the standard library relaxations, but this has not been observed in test output. **Notes:** - Any commits starting with "upstream:" can be ignored, as these correspond to other upstream PRs that this is based on which have yet to be merged. - This best reviewed commit-by-commit. I've attempted to make the implementation easy to follow and keep similar changes and test output updates together. - Each commit has a short description describing its purpose. - This patch is large but it's primarily in the test suite. - I've worked on the performance of this patch and a few optimisations are implemented so that the performance impact is neutral-to-minor. - `PointeeSized` is a different name from the RFC just to make it more obvious that it is different from `std::ptr::Pointee` but all the names are yet to be bikeshed anyway. - `@nikomatsakis` has confirmed [that this can proceed as an experiment from the t-lang side](https://rust-lang.zulipchat.com/#narrow/channel/435869-project-goals/topic/SVE.20and.20SME.20on.20AArch64.20.28goals.23270.29/near/506196491) - FCP in rust-lang/rust#137944 (comment) Fixes rust-lang/rust#79409. r? `@ghost` (I'll discuss this with relevant teams to find a reviewer)
2 parents dfb967e + cac6288 commit 78b2768

File tree

5 files changed

+16
-3
lines changed

5 files changed

+16
-3
lines changed

clippy_lints/src/methods/unnecessary_to_owned.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,11 @@ fn check_other_call_arg<'tcx>(
388388
&& let (input, n_refs) = peel_middle_ty_refs(*input)
389389
&& let (trait_predicates, _) = get_input_traits_and_projections(cx, callee_def_id, input)
390390
&& let Some(sized_def_id) = cx.tcx.lang_items().sized_trait()
391+
&& let Some(meta_sized_def_id) = cx.tcx.lang_items().meta_sized_trait()
391392
&& let [trait_predicate] = trait_predicates
392393
.iter()
393394
.filter(|trait_predicate| trait_predicate.def_id() != sized_def_id)
395+
.filter(|trait_predicate| trait_predicate.def_id() != meta_sized_def_id)
394396
.collect::<Vec<_>>()[..]
395397
&& let Some(deref_trait_id) = cx.tcx.get_diagnostic_item(sym::Deref)
396398
&& let Some(as_ref_trait_id) = cx.tcx.get_diagnostic_item(sym::AsRef)

clippy_lints/src/needless_borrows_for_generic_args.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ fn needless_borrow_count<'tcx>(
174174
) -> usize {
175175
let destruct_trait_def_id = cx.tcx.lang_items().destruct_trait();
176176
let sized_trait_def_id = cx.tcx.lang_items().sized_trait();
177+
let meta_sized_trait_def_id = cx.tcx.lang_items().meta_sized_trait();
177178
let drop_trait_def_id = cx.tcx.lang_items().drop_trait();
178179

179180
let fn_sig = cx.tcx.fn_sig(fn_id).instantiate_identity().skip_binder();
@@ -209,6 +210,7 @@ fn needless_borrow_count<'tcx>(
209210
.all(|trait_def_id| {
210211
Some(trait_def_id) == destruct_trait_def_id
211212
|| Some(trait_def_id) == sized_trait_def_id
213+
|| Some(trait_def_id) == meta_sized_trait_def_id
212214
|| cx.tcx.is_diagnostic_item(sym::Any, trait_def_id)
213215
})
214216
{

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,16 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
116116
];
117117

118118
let sized_trait = need!(cx.tcx.lang_items().sized_trait());
119+
let meta_sized_trait = need!(cx.tcx.lang_items().meta_sized_trait());
119120

120121
let preds = traits::elaborate(cx.tcx, cx.param_env.caller_bounds().iter())
121122
.filter(|p| !p.is_global())
122123
.filter_map(|pred| {
123124
// Note that we do not want to deal with qualified predicates here.
124125
match pred.kind().no_bound_vars() {
125-
Some(ty::ClauseKind::Trait(pred)) if pred.def_id() != sized_trait => Some(pred),
126+
Some(ty::ClauseKind::Trait(pred))
127+
if pred.def_id() != sized_trait && pred.def_id() != meta_sized_trait
128+
=> Some(pred),
126129
_ => None,
127130
}
128131
})

tests/ui/def_id_nocore.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
#[link(name = "c")]
88
unsafe extern "C" {}
99

10+
#[lang = "pointee_sized"]
11+
pub trait PointeeSized {}
12+
13+
#[lang = "meta_sized"]
14+
pub trait MetaSized: PointeeSized {}
15+
1016
#[lang = "sized"]
11-
pub trait Sized {}
17+
pub trait Sized: MetaSized {}
1218
#[lang = "copy"]
1319
pub trait Copy {}
1420
#[lang = "freeze"]

tests/ui/def_id_nocore.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: methods called `as_*` usually take `self` by reference or `self` by mutable reference
2-
--> tests/ui/def_id_nocore.rs:27:19
2+
--> tests/ui/def_id_nocore.rs:33:19
33
|
44
LL | pub fn as_ref(self) -> &'static str {
55
| ^^^^

0 commit comments

Comments
 (0)