Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid repr options error support #2517

Open
MahadMuhammad opened this issue Aug 3, 2023 · 0 comments
Open

Invalid repr options error support #2517

MahadMuhammad opened this issue Aug 3, 2023 · 0 comments
Assignees

Comments

@MahadMuhammad
Copy link
Contributor

// Representation options, specified via attributes e.g. #[repr(packed)]
struct ReprOptions
{
// bool is_c;
// bool is_transparent;
//...
// For align and pack: 0 = unspecified. Nonzero = byte alignment.
// It is an error for both to be nonzero, this should be caught when
// parsing the #[repr] attribute.
unsigned char align = 0;
unsigned char pack = 0;
};

I think it might be worth having some kind of error node state here. For example, during the type-checking of this structure, you could represent an error state by a flag or something so that when you are compiling the type we know if it's ok or not so we can error or ignore the options. This will only really matter down the line when we get rid of the saw_errors guards between each compiler pass.

Originally posted by @philberty in #1188 (comment)


For ReprFlags:

  • We can create an enum., like:
enum ReprFlags : unsigned int {
    IS_C,
    IS_SIMD,
    IS_TRANSPARENT,
    IS_LINEAR,
    RANDOMIZE_LAYOUT,
    IS_UNOPTIMISABLE // Not sure to add this
};

Adding error node:

  • We can add a error node in struct ReprOptions, like:
bool is_error

Raise Error while parsing the options:

  • By adding a check here:

size_t oparen = inline_option.find ('(', 0);
bool is_pack = false, is_align = false;
unsigned char value = 1;
if (oparen == std::string::npos)
{
is_pack = inline_option.compare ("packed") == 0;
is_align = inline_option.compare ("align") == 0;
}
else
{
std::string rep = inline_option.substr (0, oparen);
is_pack = rep.compare ("packed") == 0;
is_align = rep.compare ("align") == 0;
size_t cparen = inline_option.find (')', oparen);
if (cparen == std::string::npos)
{
rust_error_at (locus, "malformed attribute");
}
std::string value_str = inline_option.substr (oparen, cparen);
value = strtoul (value_str.c_str () + 1, NULL, 10);
}
if (is_pack)
repr.pack = value;
else if (is_align)
repr.align = value;
// Multiple repr options must be specified with e.g. #[repr(C,
// packed(2))].
break;
}
}


Implementation of this in rustc:

Valid rustc reprs are here:

    .help = valid reprs are `C`, `align`, `packed`, `transparent`, `simd`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`

In rustc:

@MahadMuhammad MahadMuhammad changed the title Invalid repr options error Invalid repr options error support Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant