D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2830 - private attribute doesn't work for structs/unions/classes
Summary: private attribute doesn't work for structs/unions/classes
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: All All
: P2 normal
Assignee: No Owner
URL: http://www.digitalmars.com/d/2.0/attr...
Keywords:
Depends on: 313 314
Blocks: 1441 3108
  Show dependency treegraph
 
Reported: 2009-04-10 07:50 UTC by anonymous4
Modified: 2019-07-16 04:31 UTC (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description anonymous4 2009-04-10 07:50:17 UTC
tmp1.d
---
private struct TestStruct
{
  int var;
}
---
tmp.d
---
import tmp1;

int main()
{
  TestStruct s;
  s.var=0;
  return 0;
}
---
Comment 1 Gide Nwawudu 2009-04-10 09:59:42 UTC
Structs, unions, classes and variables can bypass the private attribute.
Similar to BUG 313 and BUG 314, added dependancies.

tmp1.d
------
private struct TestStruct {
    int var;
}

private union TestUnion {
    int var;
}

private class TestClass {
    int var;
}

private int var;

tmp.d
-----
import tmp1;

void main() {
    TestStruct s;
    TestUnion u;
    auto c = new TestClass;
    s.var = var; // Fails
    s.var = a.var; // OK
}
Comment 2 Jarrett Billingsley 2009-04-10 11:17:12 UTC
This is almost certainly a dup of BUG 1441.  The same lack of privacy mechanism applies to all kinds of UDTs.  It doesn't really have anything to do with BUG 313 or BUG 314.
Comment 3 Stewart Gordon 2009-04-10 14:18:25 UTC
What is 'a'?  That line doesn't seem to have anything to say.

But this code, compiled with the same tmp1.d, has something to say:

----------
import tmp1;

void main() {
    TestStruct s;
    TestUnion u;
    auto c = new TestClass;
    auto sv = s.var;
    auto uv = u.var;
    auto cv = c.var;
    auto v = var;
}
Comment 4 Gide Nwawudu 2009-04-11 04:11:51 UTC
> tmp.d
> -----
> import tmp1;
> 
> void main() {
>     TestStruct s;
>     TestUnion u;
>     auto c = new TestClass;
>     s.var = var; // Fails
>     s.var = a.var; // OK

// Typo.
      s.var = tmp1.var; //Compiles but shouldn't bug 314

> }

Looks like a dup of bug 1440, my google-fu is weak.



Comment 5 anonymous4 2009-04-11 04:54:06 UTC
^^it's actually my google-fu.

Changing to spec bug. Spec should be clarified, how protection attributes work in this case.
Comment 6 Stewart Gordon 2009-04-12 17:53:04 UTC
Discussion taken to:
"The great inapplicable attribute debate"
digitalmars.D:87915
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=87915
Comment 7 Jonathan M Davis 2011-05-08 14:48:51 UTC
This is a bug in both D1 and D2, so I'm marking it as such. TestStruct shouldn't even be constructable outside of tmp1.d. The question of allowing you to access its member variables is secondary. It looks like the struct itself is being left as public, when it should be private. Now, it's still a bug if you can access its private member variables, but you shouldn't even be able to construct it in the first place.
Comment 8 Walter Bright 2012-01-23 00:36:23 UTC
It's not a spec bug. Private declarations should not be visible outside their module.
Comment 9 Jonathan M Davis 2012-01-23 07:51:48 UTC
> It's not a spec bug. Private declarations should not be visible outside their
> module.

Do you mean not visible or not accessible? At present, private seems to work like C++ in that it's _always_ visible but not accessible. It's included in overload sets too.
Comment 10 Walter Bright 2012-01-23 11:25:49 UTC
I meant accessible.
Comment 11 Walter Bright 2012-09-04 22:17:05 UTC
Fix for D2: https://github.com/D-Programming-Language/dmd/pull/163
Comment 12 Oleg Kuporosov 2012-10-17 22:08:03 UTC
Peter's fixes from "Comment 11" is verified in dmd2.061 alpha, but looks wasn't merged into D1 branch, so tests still failed in dmd1.076 alpha. This record wss issued against D1 so status is unchanged.
Comment 13 Mathias LANG 2019-07-16 04:31:22 UTC
Fixed in D2