Skip to content

A non-pub method is callable from another module (§18.3) #2186

Closed

Description

@ehartford

Spec §18.3: "pub exports names. No pub = module-private. Cross-module access to a non-pub symbol is a compile error; this applies uniformly to functions, types, constants, and globals."

// lib1.w
pub type Box1 { n: i32 }
impl Box1:
    fn secret() -> i32: self.n
    pub fn open() -> i32: self.n
// main.w
use lib1
let b = Box1 { n: 3 }
print(b.secret())

Expected: an error at b.secret() naming the non-pub method.
Actual: prints 3.

The stdlib relies on the hole. For example, BTreeMap.key_at is a non-pub helper over raw pointers that does no bounds check. User code can call it today: BTreeMap.new().key_at(0) segfaults (rc 139). Enforcing the rule needs an audit of the non-pub methods user programs and compiler-generated calls reach (debug_form, trait impl methods are exempt through the trait).

Activity

  1. ehartford commented on Oct 6, 2026

    @ehartford
    ContributorAuthor

    Ruled by D100 (spec v7.27, #2207): visibility is package-wide. A non-pub method is visible throughout its package and refused from another package; a module under an internal path is importable only from its parent's tree; fields follow the same rule; unchecked helpers like BTreeMap.key_at become unsafe fn.

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