Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module containing a class with the same name results in invalid generated code #4449

Open
lucasmcdonald3 opened this issue Aug 21, 2023 · 6 comments · May be fixed by #6055
Open

Module containing a class with the same name results in invalid generated code #4449

lucasmcdonald3 opened this issue Aug 21, 2023 · 6 comments · May be fixed by #6055
Labels
during 2: compilation of correct program Dafny rejects a valid program during compilation kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label lang: c# Dafny's C# transpiler and its runtime lang: java Dafny's Java transpiler and its runtime lang: js Dafny's JavaScript transpiler and its runtime part: code-generation Support for transpiling Dafny to another language. If relevant, add a `lang:` tag priority: next Will consider working on this after in progress work is done

Comments

@lucasmcdonald3
Copy link
Contributor

Dafny version

4.2.0

Code to produce this issue

module AnyName {

    class B {
        const i := 1;
        constructor() {}
    }

    class AnyName {
        const j: B

        constructor ()
        {
            this.j := new B();
        }
    }
}

Command to run and resulting output

dafny build --target:py AnyName.dfy

cd AnyName-py/
python3
Python 3.11.2 (main, Aug 10 2023, 11:27:00) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import AnyName
>>> a = AnyName.AnyName()
>>> a.ctor__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/lucmcdon/Desktop/AnyName-py/AnyName.py", line 36, in ctor__
    nw0_ = AnyName.B()
           ^^^^^^^^^
AttributeError: type object 'AnyName' has no attribute 'B'

What happened?

In the generated Python, the AnyName class calls AnyName.B() in its ctor__ function.

However, AnyName is ambiguous within this function, as it is assigned twice:

  • Once as the Dafny module on ~line 11: AnyName = sys.modules[__name__]
  • A second time as the Dafny class on ~line 28: class AnyName:

I suspect the generated Python thinks that in AnyName.B(), AnyName is referring to the module, and not the class. However, during execution, AnyName actually refers to the class here, and the call fails.

I would expect to be able to write a class that shares the same name as the module and use other classes within that module.

One workaround might be to append some uncommon string to variable containing the result of the sys.modules call, e.g. AnyName_moduleref = sys.modules[__name__] to differentiate generated references to the module from any references to classes. I modified Dafny-generated code locally to make this change and was able to run the ctor__ function.

What type of operating system are you experiencing the problem on?

Mac

@lucasmcdonald3 lucasmcdonald3 added the kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label label Aug 21, 2023
@fabiomadge fabiomadge added lang: js Dafny's JavaScript transpiler and its runtime lang: java Dafny's Java transpiler and its runtime lang: c# Dafny's C# transpiler and its runtime lang: python Dafny's Python transpiler and its runtime part: code-generation Support for transpiling Dafny to another language. If relevant, add a `lang:` tag labels Aug 21, 2023
@fabiomadge
Copy link
Collaborator

module AnyName {
  class B {
    const i := 1

    constructor() {}
  }

  class AnyName {
    const j: B

    constructor ()
    {
      this.j := new B();
    }
  }
}

method Main() {
  var b := new AnyName.B();
  var an := new AnyName.AnyName();
  print b, "\n", an, "\ndone", "\n";
}

Yes, that's an issue with all compilers, apart from Go.

@fabiomadge fabiomadge self-assigned this Aug 22, 2023
@lucasmcdonald3 lucasmcdonald3 changed the title [Python] Module containing a class with the same name results in invalid generated code Module containing a class with the same name results in invalid generated code Aug 22, 2023
@fabiomadge
Copy link
Collaborator

Fixed for Python in #4453.

@keyboardDrummer keyboardDrummer added during 2: compilation of correct program Dafny rejects a valid program during compilation priority: next Will consider working on this after in progress work is done labels Apr 24, 2024
@keyboardDrummer
Copy link
Member

Based on the test you added, it seems to have been fixed for all compilers. What makes you say it was only fixed for Python, @fabiomadge ?

@fabiomadge
Copy link
Collaborator

I wrote that, because I had to check in exceptions for cs, java, and js. They are still present and the tests pass, so I assume the issue persists for those languages.

@keyboardDrummer
Copy link
Member

I wrote that, because I had to check in exceptions for cs, java, and js. They are still present and the tests pass, so I assume the issue persists for those languages.

Can you point me to the exceptions? I looked for them and didn't see them.

@fabiomadge
Copy link
Collaborator

cs, java, and js

@fabiomadge fabiomadge removed their assignment Apr 24, 2024
@keyboardDrummer keyboardDrummer removed the lang: python Dafny's Python transpiler and its runtime label Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
during 2: compilation of correct program Dafny rejects a valid program during compilation kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label lang: c# Dafny's C# transpiler and its runtime lang: java Dafny's Java transpiler and its runtime lang: js Dafny's JavaScript transpiler and its runtime part: code-generation Support for transpiling Dafny to another language. If relevant, add a `lang:` tag priority: next Will consider working on this after in progress work is done
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants