[Issue 12059] New: Smarter error messages when a module contains a namespace with the same name

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Feb 2 02:35:26 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=12059

           Summary: Smarter error messages when a module contains a
                    namespace with the same name
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: diagnostic
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2014-02-02 02:35:15 PST ---
This is just one example of a problem in D code that I have seen seen several
times in D.learn and elsewhere:

http://forum.dlang.org/thread/mailman.28.1391288417.2683.digitalmars-d-learn@puremagic.com

-------------

> I am having troubles to use the enum defined in the separate 
> module.
> When I try to access it, I am getting "Undefined symbol" error:
>
>
> // CodeEnum.d
>
> enum CodeEnum
> {
> 	OK = 200,
> 	FAIL = 400
> }
>
> unittest
> {
> 	auto e = CodeEnum.OK; // Works!
> }
>
>
> // Reply.d
> import CodeEnum;
>
> unittest
> {
> 	auto.e = CodeEnum.OK; // Error: undefined identifier 'OK'
> }
>
>
> What I am doing wrong?

-------------

The answer that explains the problem:

> The module and your enum have the same name. When the compiler sees
> the `CodeEnum` symbol, it considers you're referring to the module.
> This module does not have an `OK` member, hence the error.
> In D, do not use the same symbol for a module and one of its inner symbols.

-------------

Lot of time ago I was hit by a similar problem defining a "set.d" module with
inside a "Set" struct plus a "set()" helper function.

There are various ways to avoid this problem. One way is to always forbid to
define the name "foo" inside the module named "foo", with an error message
(Like: "Error: module foo contains a member named foo. Module members cannot
shadow module names" as suggested by Philippe Sigaud).

I like that idea, but it's a significant breaking change, and it looks quite
restrictive.

An alternative solution that is much less disruptive change is to just improve
the error message. Instead of just giving "Error: undefined identifier 'OK'" a
more descriptive error message can tell the programmer what the problem exactly
is and how to fix:

Error: undefined identifier 'OK' inside module 'CodeEnum.d'. Did you mean to
use 'CodeEnum.CodeEnum.OK'?

This error message does not avoid the problems, so it's less good than
forbidding the name duplication, but it could be enough.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list