Skip to content

pub const still requires an explicit type; the value already forces it #2101

Closed

Description

@ehartford

Repro (with v0.15.3.0)

pub const ENEMY_CAP = 2000
pub const STEP = 1.0 / 120.0
pub const BOSS_NAMES = ["WARDEN", "LANCER", "HIVE"]
pub const VALVE_VENDOR = 0x28DEu16
error: public const declarations require an explicit type annotation
 --> game.w:12:21
12 | pub const ENEMY_CAP = 2000

The rule comes from aa6e50ae ("Allow inferred private and local const", #353, Jun 9), which allowed inference for private and local const and kept it out of pub const (test/compile_errors/err_pub_const_requires_type.w). The idiomatic guide still says "const requires a type annotation" (docs/spec/guide/with-idiomatic-guide.md:190).

Why it should go (mission.md)

  • The value forces exactly one type. 2000 is i32, 1.0 / 120.0 is f64, and 0x28DEu16 is u16. Writing : i32 repeats "what the program has already determined", which the mission calls a compiler failure. The type is no less fixed for being visible to another module.
  • The function rule already moved. Public functions infer their return types now ("public return inference", Sep 25). A public const is the simpler case: its type comes from one literal or constant expression, not from control flow.
  • It leaves a two-tier style. WIPE's private constants now read const CELL_SIZE = 64, but its public ones must read pub const ENEMY_CAP: i32 = 2000.

A literal suffix covers the cases where the wanted type differs from the literal's default (1u32, 0x28DEu16), so the annotation is never the only way to say it.

Expected

pub const NAME = <constant expression> takes the expression's type, like a private const. Update the guide's sentence to match.

Found updating withlang-dev/wipe. Related: #2096 (float const contextual type), #2097 (field defaults).

Activity

  1. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by stack #2196 (merged as efb765c), battery green on a21e326; the regression test for it landed with the fix.

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