Skip to content

Allow inferred private and local const declarations #353

Closed

Description

@ehartford

Summary

The spec now allows const NAME = EXPR when the initializer determines an unambiguous type. const NAME: TYPE = EXPR remains available for API clarity and disambiguation, and public exported constants must keep an explicit type annotation.

Current behavior

The parser still requires a type annotation for every const declaration.

Observed with ./out/bin/with -e:

./out/bin/with -e 'const X = 1; let y = X'
# error: const declaration requires a type annotation

./out/bin/with -e 'const X: i32 = 1; let y = X'
# accepted

Public untyped constants should remain rejected, but the diagnostic should be the narrower rule:

pub const X = 1
// error: public const declarations require an explicit type annotation

Suspected root cause

src/Parser.w rejects missing : in both const parser paths:

  • parse_const_decl (module/global declarations)
  • parse_const_binding (local bindings)

Both currently emit const declaration requires a type annotation before sema can infer from the initializer.

Expected behavior

  • Accept private/module const NAME = EXPR when the initializer determines a concrete type using normal inference and default literal rules.
  • Accept local const NAME = EXPR under the same rule.
  • Keep const NAME: TYPE = EXPR working.
  • Reject public/exported pub const NAME = EXPR with a diagnostic that says public constants require explicit type annotations for API stability.
  • Reject untyped const declarations whose initializer cannot determine a concrete type.
  • Add behavior and compile-error coverage.

Spec references

  • docs/with-specification.md §9.1b const Declarations
  • docs/requirements.md 9.1.3.2 and 9.1.3.3

Activity

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