Hasty Briefsbeta

Bilingual

The perils of parsing type inference declarations in C

6 hours ago
  • The `auto` keyword in C can be either a storage-class specifier or a type inference placeholder, creating parsing ambiguity when combined with a typedef name `x`.
  • GCC treats `x` as a typedef, causing an error for `auto x = 67;`, while Clang looks ahead and parses it correctly.
  • C23 explicitly disambiguates: if a typedef identifier is redeclared in an inner scope, its type shall not be inferred. Despite this, GCC and Clang still diverge in practice.
  • Adding attributes (e.g., `auto x [[attr]] y = 67;`) further complicates parsing: only GCC succeeds, while Clang misapplies attributes to the variable `x`.
  • Clang supports array, function, and pointer declarators with type inference (implementation-defined), leading to ambiguous cases like `auto x (y) = 67;`.
  • The `__auto_type` extension in GCC and Clang has different scope semantics: GCC does not insert the new declaration until after the initializer, whereas Clang uses C23's underspecified declaration rules.