Repository navigation
Description
Summary
const and enum values are inlined at transpile time, and bsc resolves them against the bs_const manifest it builds with. When the declaration is inside a conditional compile block, a single inlined value often can't be correct for every branch. This proposes restricting where those declarations can be used. It came out of #1884.
The problem
#if DEBUG
const LEVEL = 1
#else
const LEVEL = 2
#end if
sub main()
#if DEBUG
print LEVEL
#else
print LEVEL
#end if
end subWith bs_const=DEBUG=true, the fix for #1884 lets the live declaration win, so both usages become print 1. That is correct for the manifest bsc built with. However, the #else branch ships as print 1 even though its source says LEVEL = 2, so the output is wrong if the app ships with a different bs_const. (Before #1884, the last declaration won, so the live branch printed 2.)
Resolving each usage against the declaration in its own branch only works in some cases:
- A usage outside any
#if(print LEVELdirectly inmain) is either 1 or 2 depending onDEBUG. The only way to emit that is to duplicate the statement under#if, which grows exponentially with independent flags and is impossible inside an expression. - A usage under an unrelated flag (
#if VERBOSE) says nothing aboutDEBUG. - A usage in another file can't follow that file's own
#constvalues at all, since#constis per file.
Options
1. Disallow const and enum declarations inside conditional compile blocks.
Simple, but it also flags #if false blocks used to comment out code that happens to declare a const or enum.
2. Allow them, but only inside the block that declares them.
A const or enum declared in an #if branch can only be referenced from inside that same branch:
#if DEBUG
const LEVEL = 1
sub logIt()
print LEVEL ' ok, always 1
end sub
#else
const LEVEL = 2
sub logIt()
print LEVEL ' ok, always 2
end sub
#end if
sub main()
print LEVEL ' error: used outside its conditional compile block
end subEvery usage that is allowed has exactly one possible value, so dead branches stay correct under any manifest. #if false blocks keep working as comments as long as their usages are self-contained. This needs branch-aware lookups, so each usage resolves to the declaration whose branch encloses it.
3. Like 2, but allow usages whose own #if path settles every flag the declaration depends on.
For example, a later #if DEBUG block could use the DEBUG branch's LEVEL. This needs a symbolic evaluator over the path conditions, including negations from #else if chains and file #const values set inside branches.
Open questions
- Should 2 or 3 apply to enums used only as types (
sub foo(c as Color)), which are not inlined? - Should declaring the same name both inside a block and unconditionally be a duplicate error?
- Error or warning for existing projects?
- Do real projects rely on per-flag config values (declare per flag at the top, use everywhere)? Options 1 and 2 both disallow that pattern.
Leaning toward option 2.
Activity
TwitchBronBron commentedon Oct 7, 2026
Another option here, is to actually transpile the consts as functions. that way it still works for everyone. we should then add a diagnostic warning devs that this is no longer a transpile-time optimization. it would create something like this: