Skip to content

Invalid repr options error support #2517

@MahadMuhammad

Description

@MahadMuhammad

// 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:

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions