D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4533 - Ban public aliases to private symbols
Summary: Ban public aliases to private symbols
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2010-07-29 13:59 UTC by Tomasz Sowiński
Modified: 2020-08-06 14:28 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tomasz Sowiński 2010-07-29 13:59:12 UTC
This module compiles fine...:

module A;

private void foo();
public alias foo goo;

... but try to use goo...:

module B;
import A;

void main() {
    goo();
}

... and an error comes up:

Error: function A.foo is not accessible from B

We had a long discussion on digitalmars.D whether visibility-expanding aliases should be functional or not. If they were allowed, in some cases (member functions) the very presence of the alias could alter the generated code (think of class invariants, exposing public members to exe/dll, etc). So the conclusion was to cut the problem in the nip and retain the primary idea of aliases as mere helper symbol references whose existence ought not to be manifested in binary code.

In other words, an alias whose visibility > than aliased symbol should fail already on the declaration spot.
Comment 1 Nick Sabalausky 2010-07-29 16:04:27 UTC
The opinion of the "should be allowed" side is that there are real cases where it's useful. Another option would be to allow it, but only for cases where it wouldn't affect code generation.

In any case, the behavior of the compiler does need to be changed because it's currently disallowed but without a clear error message on the line where programmer tries create the alias.

For reference, the newsgroup discussion is here:
http://www.mail-archive.com/digitalmars-d@puremagic.com/msg34092.html
Comment 2 Leandro Lucarella 2010-07-29 16:54:37 UTC
The problem is, attributes in D are not as in Java, they can be applied to a lot of symbols, like:

---
private int j;

public:

int i;
// another bunch of stuff
alias j k;
---

Should the compiler complain at that alias? What about this:

---
void f() {}

extern (C):

void g() {}
// another bunch of stuff
alias f h;
---

Should the compiler complain about non-sense extern (C) alias? What about const? You can "revert" the extern (C) with extern (D) and public with private, but there is no "mutable" to revert const.

This is a bigger problem on how attributes work in D, and there were some threads about the issue.

And I'm not saying I'm against issuing error when attributes are applied to things that doesn't make sense, I'm just sharing some problems about the issue :)
Comment 3 Tomasz Sowiński 2010-07-30 13:52:33 UTC
(In reply to comment #2)
> The problem is, attributes in D are not as in Java, they can be applied to a
> lot of symbols, like:
> 
> ---
> private int j;
> 
> public:
> 
> int i;
> // another bunch of stuff
> alias j k;
> ---
> 
> Should the compiler complain at that alias? What about this:

Of course, accessed from a different module it fails like my example.
 
> ---
> void f() {}
> 
> extern (C):
> 
> void g() {}
> // another bunch of stuff
> alias f h;
> ---
> 
> Should the compiler complain about non-sense extern (C) alias? What about
> const? You can "revert" the extern (C) with extern (D) and public with private,
> but there is no "mutable" to revert const.

Yes, it should complain. I think it doesn't matter that there's no "revert". If it's wrong then apply a different attribute or move it outside the attribute scope or whatever, just fix it :)

> This is a bigger problem on how attributes work in D, and there were some
> threads about the issue.
> 
> And I'm not saying I'm against issuing error when attributes are applied to
> things that doesn't make sense, I'm just sharing some problems about the issue
> :)

I wasn't aware of problems with attributes in general, thanks for bringing this up. That just made me try:

const alias char C;   // C is char, not const(char)

Also, you can alias members from *outside*, which seems pointless and weird:

class A {
    void foo() {}
}

alias A.foo goo;
Comment 4 anonymous4 2010-07-31 05:04:59 UTC
(In reply to comment #0)
> This module compiles fine...:
> 
> module A;
> 
> private void foo();
> public alias foo goo;
> 
> ... but try to use goo...:
> 
> module B;
> import A;
> 
> void main() {
>     goo();
> }
> 
> ... and an error comes up:
> 
> Error: function A.foo is not accessible from B

> So the
> conclusion was to cut the problem in the nip and retain the primary idea of
> aliases as mere helper symbol references

This is how it works. It doesn't matter, whether the alias is public or not, alias is still public, it doesn't help you access private members, as you can access them directly:

module B;
import A;

void main() {
    A.foo();
}

I think, if you access only alias, in, say, some sort of metaprogramming, you won't touch foo and your code will be valid.
Comment 5 Andrej Mitrovic 2010-08-01 07:10:23 UTC
Is the following relevant to this bug report?

In the docs (http://www.digitalmars.com/d/2.0/declaration.html), there's this code:

void main() {
    struct S { static int i; }
    S s;

    alias s.i a;	// illegal, s.i is an expression
    alias S.i b;	// ok
    b = 4;		// sets S.i to 4
}

But this will compile. I'm not sure if it's relevant to this bug report or if I should open up a new one?
Comment 6 Tomasz Sowiński 2010-08-01 07:57:18 UTC
(In reply to comment #5)
> Is the following relevant to this bug report?
> 
> In the docs (http://www.digitalmars.com/d/2.0/declaration.html), there's this
> code:
> 
> void main() {
>     struct S { static int i; }
>     S s;
> 
>     alias s.i a;    // illegal, s.i is an expression
>     alias S.i b;    // ok
>     b = 4;        // sets S.i to 4
> }
> 
> But this will compile. I'm not sure if it's relevant to this bug report or if I
> should open up a new one?

Good catch. I opened bug 4545.
Comment 7 Martin Nowak 2012-01-31 14:27:04 UTC
This should be fixed to work with functions as it already works
for other declarations.
Comment 8 Mathias LANG 2020-08-06 14:28:53 UTC
Protection has been turned into visibility, and having something that exposes publicly something that is originally private is a common use case. It now works correctly for all types, so closing as INVALID.