Skip to content

Commit

Permalink
Merge pull request #509 from FStarLang/protz_collision
Browse files Browse the repository at this point in the history
Fix collisions between local variables and C keywords
  • Loading branch information
msprotz authored Dec 23, 2024
2 parents f1d7a3e + 7e5d129 commit 3823e3d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 113 deletions.
1 change: 1 addition & 0 deletions lib/AstToCStar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ let ensure_fresh env name body cont =
!r
in
mk_fresh name (fun tentative ->
List.mem tentative GlobalNames.keywords ||
tricky_shadowing_see_comment_above tentative body 0 ||
List.exists (fun cont -> tricky_shadowing_see_comment_above tentative (Some cont) 1) cont ||
List.mem tentative env.in_block ||
Expand Down
225 changes: 113 additions & 112 deletions lib/GlobalNames.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,119 +19,120 @@ let dump (env: t) =
KPrint.bprintf "%s is used\n" c_name
) (snd env)

let keywords = [
"_Alignas";
"_Alignof";
"_Atomic";
"_Bool";
"_Complex";
"_Generic";
"_Imaginary";
"_Noreturn";
"_Pragma";
"_Static_assert";
"_Thread_local";
"alignas";
"alignof";
"and";
"and_eq";
"asm";
"atomic_cancel";
"atomic_commit";
"atomic_noexcept";
"auto";
"bitand";
"bitor";
"bool";
"break";
"case";
"catch";
"char";
"char16_t";
"char32_t";
"char8_t";
"class";
"co_await";
"co_return";
"co_yield";
"compl";
"concept";
"const";
"const_cast";
"consteval";
"constexpr";
"constinit";
"continue";
"decltype";
"default";
"delete";
"do";
"double";
"dynamic_cast";
"else";
"enum";
"explicit";
"export";
"extern";
"false";
"float";
"for";
"fortran";
"friend";
"goto";
"if";
"inline";
"int";
"long";
"mutable";
"namespace";
"new";
"noexcept";
"not";
"not_eq";
"nullptr";
"operator";
"or";
"or_eq";
"private";
"protected";
"public";
"reflexpr";
"register";
"reinterpret_cast";
"requires";
"restrict";
"return";
"short";
"signed";
"sizeof";
"static";
"static_assert";
"static_cast";
"struct";
"switch";
"synchronized";
"template";
"this";
"thread_local";
"throw";
"true";
"try";
"typedef";
"typeid";
"typename";
"union";
"unsigned";
"using";
"virtual";
"void";
"volatile";
"wchar_t";
"while";
"xor";
"xor_eq";
]

let reserve_keywords used_c_names =
let keywords = [
"_Alignas";
"_Alignof";
"_Atomic";
"_Bool";
"_Complex";
"_Generic";
"_Imaginary";
"_Noreturn";
"_Pragma";
"_Static_assert";
"_Thread_local";
"alignas";
"alignof";
"and";
"and_eq";
"asm";
"atomic_cancel";
"atomic_commit";
"atomic_noexcept";
"auto";
"bitand";
"bitor";
"bool";
"break";
"case";
"catch";
"char";
"char16_t";
"char32_t";
"char8_t";
"class";
"co_await";
"co_return";
"co_yield";
"compl";
"concept";
"const";
"const_cast";
"consteval";
"constexpr";
"constinit";
"continue";
"decltype";
"default";
"delete";
"do";
"double";
"dynamic_cast";
"else";
"enum";
"explicit";
"export";
"extern";
"false";
"float";
"for";
"fortran";
"friend";
"goto";
"if";
"inline";
"int";
"long";
"mutable";
"namespace";
"new";
"noexcept";
"not";
"not_eq";
"nullptr";
"operator";
"or";
"or_eq";
"private";
"protected";
"public";
"reflexpr";
"register";
"reinterpret_cast";
"requires";
"restrict";
"return";
"short";
"signed";
"sizeof";
"static";
"static_assert";
"static_cast";
"struct";
"switch";
"synchronized";
"template";
"this";
"thread_local";
"throw";
"true";
"try";
"typedef";
"typeid";
"typename";
"union";
"unsigned";
"using";
"virtual";
"void";
"volatile";
"wchar_t";
"while";
"xor";
"xor_eq";
] in
List.iter (fun k -> Hashtbl.add used_c_names k ()) keywords;
(* Some stuff that's already almost always in scope. *)
let std = [
Expand Down
2 changes: 2 additions & 0 deletions lib/GlobalNames.mli
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ val pascal_case: string -> string
val camel_case: string -> string

val skip_prefix: Ast.lident -> bool

val keywords: string list
5 changes: 4 additions & 1 deletion test/NameCollision.fst
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ let mk = NameCollisionHelper.mk

open NameCollisionHelper

let f (): FStar.HyperStack.ST.St Int32.t = 0l

let main () =
(mk 0l).y
let unsigned = f () in
(mk unsigned).y

0 comments on commit 3823e3d

Please sign in to comment.