Skip to content

Conversation

crusso
Copy link
Contributor

@crusso crusso commented Oct 1, 2025

This is a minor tweak of the contextual dots that avoids having to rename classing fields and method names, provided the field has a non-function type.

A bit hacky but avoids the problem with size fields and methods in core with contextual dot, without breaking the code of existing core users.

One potential drawback, besides minor complexity, is that, for generic fields, the resolution of the contextual dot may depend on the instantiation of type parameters.

Happy path:

module Map {
   public type Map<T, U> = {
      var size: Nat;
      var other: Nat
   };
   public type Self<T, U> = Map<T, U>;
   public func empty<T, U>() : Map<T, U> = { var size = 0 };
   public func size<T, U>(map : Map<T,U>) : Nat { map.size };
};

let m = Map.empty<Nat, Text>();

let n1 = m.size(); // call method
let n2 = m.size; // read field

let i1 = m.other; // read field
let i2 = m.other(); // call unavailable method

Unhappy (rare?) corner case with generic fields instantiated at function types:

module Box {
  type Box<T> = {
    value : T;
  };
  public func Box<T>(t : T) : Box<T> = { value = t };
  public type Self<T> = Box<T>;
  public func value<T>(self : Self<T>) : T { self.value };
};

let b1 = Box.Box<Nat>(1);
let n1 = b1.value;   // accept, field ref
let n2 = b1.value(); // accept, method ref

let b2 = Box.Box<Nat->Nat>(func x = x);
let f1 = b2.value; // accept, field ref
// both f2 and f3 are perhaps unexpected
let f2 = b2.value(1); // accept, field ref (applies the field of type Nat->Nat to 1)
let f3 = b2.value(); // reject, field ref (applies the field of type Nat->Nat to ())

Copy link
Contributor

github-actions bot commented Oct 3, 2025

Comparing from dea616b to b26f9c6:
The produced WebAssembly code seems to be completely unchanged.
In terms of gas, no changes are observed in 5 tests.
In terms of size, no changes are observed in 5 tests.

@crusso crusso marked this pull request as ready for review October 5, 2025 20:27
@crusso crusso requested a review from a team as a code owner October 5, 2025 20:27
@christoph-dfinity
Copy link
Contributor

Implementation looks good. Maybe we could use this as an interim fix to allow us to release without a breaking change to core, but roll it out with a warning on functions defined in modules with a Self parameter that has a clashing field?

That would leave ourselves the option of backing it out in the future.

Copy link
Contributor

@christoph-dfinity christoph-dfinity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I'll write the code to report the warning later.

@crusso crusso enabled auto-merge (squash) October 13, 2025 13:35
@crusso crusso merged commit af47340 into master Oct 13, 2025
20 checks passed
@crusso crusso deleted the claudio/contextual-dot-tweak branch October 13, 2025 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants