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

Should a module importing itself be disallowed? #3079

Open
paulyoung opened this Issue Sep 19, 2017 · 3 comments

Comments

Projects
None yet
4 participants
@paulyoung
Copy link
Contributor

paulyoung commented Sep 19, 2017

We use PureScript with webpack via purs-loader and circular-dependency-plugin, and started seeing this warning:

WARNING in Circular dependency detected:
bower_components/purescript-formatters/src/Data/Formatter/Parser/Number.purs -> bower_components/purescript-formatters/src/Data/Formatter/Parser/Number.purs

Sure enough, purescript-formatters has an import statement for itself with a function defined in the same module:

https://github.com/slamdata/purescript-formatters/blob/859e9f5c7d507441d495870acbb06380940eda97/src/Data/Formatter/Parser/Number.purs#L12

Should this be disallowed?

@paulyoung

This comment has been minimized.

Copy link
Contributor

paulyoung commented Sep 19, 2017

@natefaubion pointed out that this import doesn't appear in the generated JavaScript in Try PureScript, so maybe this is just an issue for purescript-formatters.

@natefaubion

This comment has been minimized.

Copy link
Contributor

natefaubion commented Sep 19, 2017

The compiler allows the import, but you can't reference anything in it (if qualified), and no code is generated for it. purs-loader does it's own import parsing to build dependencies for Webpack to follow, and it's adding the self reference as a dependency, which is how the plugin picks it up. I think the compiler should probably error on the import, though.

@paf31 paf31 added bug modules labels Sep 20, 2017

@paf31 paf31 added this to the Approved milestone Sep 20, 2017

@damncabbage

This comment has been minimized.

Copy link
Contributor

damncabbage commented Oct 9, 2017

A possible alternative: could importing and referring to the current module just be syntax sugar?

module Abc where

import Abc as Abc
import Def as Def

foo :: String
foo = "hi"

bar :: String
bar = Def.qux <> Abc.foo -- `Abc.foo` just turns into `foo` on compilation.

It seems ridiculous at face value, but it's useful in some code-generation scenarios where it's a pain to keep track of the "current" module. If you always blindly import what you need in each place (and if the tree of imports doesn't contain any cycles outside of self-references), then you don't have to do the current-context accounting.

(I'm aware this use-case probably isn't enough to swing it. We're taking advantage of the above behaviour for some Haskell codegen, and switched to open-imports-with-compiler-warnings-disabled for the PureScript side of things until we can do a proper job of the accounting.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment