Skip to content

Restrict const and enum declared inside conditional compile blocks to usage within that block #1885

Open

Description

@chrisdp

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 sub

With 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 LEVEL directly in main) is either 1 or 2 depending on DEBUG. 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 about DEBUG.
  • A usage in another file can't follow that file's own #const values at all, since #const is 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 sub

Every 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

  1. TwitchBronBron commented on Oct 7, 2026

    @TwitchBronBron
    Member

    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:

    function LEVEL()
      #if DEBUG
        return 1
      #else
        return 2
      #end if
    end function
    
    'And then we'd change the const refs to function calls
    function main()
        print LEVEL()
    end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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