Skip to content

Commit 4b60f89

Browse files
committed
feature/passes: add rustc_non_const_sized
While there are no non-`const Sized` types in the language, this testing attribute will make it possible to force this for testing purposes.
1 parent dcae821 commit 4b60f89

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,10 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
10131013
rustc_force_inline, Normal, template!(Word, NameValueStr: "reason"), WarnFollowing, EncodeCrossCrate::Yes,
10141014
"#[rustc_force_inline] forces a free function to be inlined"
10151015
),
1016+
rustc_attr!(
1017+
rustc_non_const_sized, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes,
1018+
"#[rustc_non_const_sized] forces a type to not implement `const {Meta,Pointee,}Sized` for testing"
1019+
),
10161020

10171021
// ==========================================================================
10181022
// Internal attributes, Testing:

compiler/rustc_passes/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,10 @@ passes_rustc_lint_opt_ty =
688688
`#[rustc_lint_opt_ty]` should be applied to a struct
689689
.label = not a struct
690690
691+
passes_rustc_non_const_sized =
692+
attribute should be applied to a struct or enum
693+
.label = not an struct or enum
694+
691695
passes_rustc_pub_transparent =
692696
attribute should be applied to `#[repr(transparent)]` types
693697
.label = not a `#[repr(transparent)]` type

compiler/rustc_passes/src/check_attr.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
217217
[sym::rustc_has_incoherent_inherent_impls, ..] => {
218218
self.check_has_incoherent_inherent_impls(attr, span, target)
219219
}
220+
[sym::rustc_non_const_sized, ..] => {
221+
self.check_rustc_non_const_sized(span, target)
222+
}
220223
[sym::ffi_pure, ..] => self.check_ffi_pure(attr.span(), attrs, target),
221224
[sym::ffi_const, ..] => self.check_ffi_const(attr.span(), target),
222225
[sym::link_ordinal, ..] => self.check_link_ordinal(attr, span, target),
@@ -2578,6 +2581,18 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
25782581
}
25792582
}
25802583

2584+
/// `#[rustc_non_const_sized]` is a testing attribute for forcing `const Sized` not to be
2585+
/// implemented (while there are no types which are non-`const Sized`) - it should only be
2586+
/// applied to structs or enums.
2587+
fn check_rustc_non_const_sized(&self, span: Span, target: Target) {
2588+
match target {
2589+
Target::Struct | Target::Enum => (),
2590+
_ => {
2591+
self.dcx().emit_err(errors::RustcNonConstSized { attr_span: span });
2592+
}
2593+
}
2594+
}
2595+
25812596
/// Checks if `#[autodiff]` is applied to an item other than a function item.
25822597
fn check_autodiff(&self, _hir_id: HirId, _attr: &Attribute, span: Span, target: Target) {
25832598
debug!("check_autodiff");

compiler/rustc_passes/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,13 @@ pub(crate) struct RustcForceInline {
690690
pub span: Span,
691691
}
692692

693+
#[derive(Diagnostic)]
694+
#[diag(passes_rustc_non_const_sized)]
695+
pub(crate) struct RustcNonConstSized {
696+
#[primary_span]
697+
pub attr_span: Span,
698+
}
699+
693700
#[derive(Diagnostic)]
694701
#[diag(passes_rustc_force_inline_coro)]
695702
pub(crate) struct RustcForceInlineCoro {

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,6 +1784,7 @@ symbols! {
17841784
rustc_never_returns_null_ptr,
17851785
rustc_never_type_options,
17861786
rustc_no_mir_inline,
1787+
rustc_non_const_sized,
17871788
rustc_nonnull_optimization_guaranteed,
17881789
rustc_nounwind,
17891790
rustc_object_lifetime_default,

0 commit comments

Comments
 (0)