close
Skip to content

Add Align type with const generic controlled alignment#154730

Draft
beepster4096 wants to merge 1 commit intorust-lang:mainfrom
beepster4096:align_lang_type
Draft

Add Align type with const generic controlled alignment#154730
beepster4096 wants to merge 1 commit intorust-lang:mainfrom
beepster4096:align_lang_type

Conversation

@beepster4096
Copy link
Copy Markdown
Contributor

@beepster4096 beepster4096 commented Apr 2, 2026

Added:

// core::mem
#[lang = "align_type"]
pub struct Align<const N: usize>;

Unresolved problems/questions:

  • The errors for invalid alignments are terrible
  • Align<N> with invalid N will compile as long as its layout is never needed (see tests\ui\layout\align-type\invalid-unused.rs) Should this happen?
  • Align<0> is valid and has an alignment of 1. Is this okay?

r? scottmcm

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 2, 2026
@@ -0,0 +1,21 @@
// FIXME: should this pass?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My instinct is that it definitely shouldn't pass, but maybe it doesn't fail elegantly -- perhaps it fails in the same way that [u32; usize::MAX / 4] fails, for example.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, types that are too large don't fail in these positions either

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, because it's just a check build or something?

If it's consistent with malformed arrays that's probably fine -- for now at least.


let mut repr = def.repr();

// FIXME: should this have a flag on the adtdef?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My instinct is no, because everything other than layout should just treat it as a normal type with a generic-dependent layout, never switch on some type flag to detect it.

const MAX_SUPPORTED_ALIGN: usize = 1 << 29;

const _: () = {
// FIXME: should this fail?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, same as #[repr(align(0))] and mem::Alignment::new(0).unwrap().

(And how https://doc.rust-lang.org/std/primitive.usize.html#method.is_power_of_two does not treat zero as a power of two.)

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 15, 2026

☔ The latest upstream changes (presumably #139087) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants