Skip to content

Commit 0fde1e2

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 87382da commit 0fde1e2

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,10 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
10201020
rustc_force_inline, Normal, template!(Word, NameValueStr: "reason"), WarnFollowing, EncodeCrossCrate::Yes,
10211021
"#[rustc_force_inline] forces a free function to be inlined"
10221022
),
1023+
rustc_attr!(
1024+
rustc_non_const_sized, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes,
1025+
"#[rustc_non_const_sized] forces a type to not implement `const {Meta,Pointee,}Sized` for testing"
1026+
),
10231027

10241028
// ==========================================================================
10251029
// 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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
218218
[sym::rustc_has_incoherent_inherent_impls, ..] => {
219219
self.check_has_incoherent_inherent_impls(attr, span, target)
220220
}
221+
[sym::rustc_non_const_sized, ..] => {
222+
self.check_rustc_non_const_sized(span, target)
223+
}
221224
[sym::ffi_pure, ..] => self.check_ffi_pure(attr.span(), attrs, target),
222225
[sym::ffi_const, ..] => self.check_ffi_const(attr.span(), target),
223226
[sym::link_ordinal, ..] => self.check_link_ordinal(attr, span, target),
@@ -2600,6 +2603,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
26002603
}
26012604
}
26022605

2606+
/// `#[rustc_non_const_sized]` is a testing attribute for forcing a type to only be `Sized`,
2607+
/// not `const Sized` (which almost all types implement) - it should only be applied to structs
2608+
/// or enums. This isn't equivalent to `!const Sized` or anything like that, it just means that
2609+
/// there isn't a `impl const Sized for T`, only `impl Sized for T` in the compiler.
2610+
fn check_rustc_non_const_sized(&self, span: Span, target: Target) {
2611+
match target {
2612+
Target::Struct | Target::Enum => (),
2613+
_ => {
2614+
self.dcx().emit_err(errors::RustcNonConstSized { attr_span: span });
2615+
}
2616+
}
2617+
}
2618+
26032619
/// Checks if `#[autodiff]` is applied to an item other than a function item.
26042620
fn check_autodiff(&self, _hir_id: HirId, _attr: &Attribute, span: Span, target: Target) {
26052621
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
@@ -1785,6 +1785,7 @@ symbols! {
17851785
rustc_never_returns_null_ptr,
17861786
rustc_never_type_options,
17871787
rustc_no_mir_inline,
1788+
rustc_non_const_sized,
17881789
rustc_nonnull_optimization_guaranteed,
17891790
rustc_nounwind,
17901791
rustc_object_lifetime_default,

0 commit comments

Comments
 (0)