Skip to content

Field projections may fail due to open namespaces in special circumstances #16044

Open

Description

@thorimur

Prerequisites

Description

Field projection resolution can unexpectedly fail due to open namespaces.

When there is a structure protected Special.Foo and a def Foo := Special.Foo, terms of type (_root_.)Foo cannot use .-notation to access Special.Foo's fields when Special is open (but can otherwise).

Context

This was noticed in mathlib when trying to move from numeric projections to readable field name projections. Mathlib has protected SubModule.IsAssociatedPrime as well as IsAssociatedPrime, and uses the former to help with the latter in this way (using a numeric projection) in the theorem IsAssociatedPrime.isPrime. Attempting to use the named field instead of the numeric projection fails.

Steps to Reproduce

namespace Special

-- 1. Define `Special.Foo`
protected structure Foo (n : Nat) : Prop where a : 0 < n

end Special

-- 2. Define top-level `Foo`
def Foo (n : Nat) : Prop := Special.Foo n

-- Note: works, yields `Special.Foo.a`
example (h : Foo 1) : 0 < 1 := h.a

-- Note: works, also yields `Special.Foo.a`
open Special in
example (h : Foo 1) : 0 < 1 := h.1

-- 3.
/--
error: Invalid field notation: Function `Foo.a` does not have a usable parameter of type `Foo ...` for which to substitute `h`

Note: Such a parameter must be explicit, or implicit with a unique name, to be used by field notation
-/
#guard_msgs in
open Special in
example (h : Foo 1) : 0 < 1 := h.a

(One can also remove protected in exchange for (h : _root_.Foo 1), but I figure protected is closer to acceptable practice.)

Expected behavior: All examples succeed, since there is no ambiguity even when open is present.

Actual behavior: open Special in example (h : Foo 1) : 0 < 1 := h.a fails.

Versions

4.37.0-nightly-2026-10-27
Lean web

Additional Information

This may have been inadvertently introduced when improving dot notation in #6189.

The issue roughly seems to be that the open Special allows us to resolve a to Special.Foo.a correctly by respecting opens, but succeeding here causes us to avoid unfolding Foo (which we need to do in order to recognize that the parameter has an acceptable type).

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Activity

  1. added
    bug
    on Oct 27, 2026
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions