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

Dereference on single element tags ignores visibility of constructor #818

Closed
msullivan opened this issue Aug 12, 2011 · 6 comments
Closed
Labels
A-type-system Area: Type system

Comments

@msullivan
Copy link
Contributor

Exporting a tag type but not the constructors for it is a simple way to construct abstract data types. (This is how it is done in Haskell.) This works in rust as well, but only for tags with multiple constructors. If the tag has only a single constructor, * will see through it regardless of the visibility of the constructor.

Example

mod ctr {
    export ctr;
    export new; export inc; export print;

    tag ctr { mkCtr(int); }

    fn new(i: int) -> ctr { mkCtr(i) }
    fn inc(c: ctr) -> ctr { mkCtr(*c + 1) }
    fn print(c: ctr) { log_err *c; }
}


fn main() {
    let c = ctr::new(42);
    ctr::print(c);
    let c2 = ctr::inc(c);
    ctr::print(c2);
    log_err *c2;
}

Part of the problem might be that dereference on single element tags isn't a particularly principled idea to begin with, but it is undeniably convenient.

@msullivan
Copy link
Contributor Author

This is one of two interrelated complaints in #802.

@brson
Copy link
Contributor

brson commented Feb 26, 2012

Updated test case:

mod ctr {
    export ctr::{};
    export new; export inc; export print;

    enum ctr { mk_ctr(int) }

    fn new(i: int) -> ctr { mk_ctr(i) }
    fn inc(c: ctr) -> ctr { mk_ctr(*c + 1) }
    fn print(c: ctr) { log(error, *c); }
}


fn main() {
    let c = ctr::new(42);
    ctr::print(c);
    let c2 = ctr::inc(c);
    ctr::print(c2);
    log(error, *c2);
}

@ghost ghost assigned catamorphism May 3, 2012
@catamorphism
Copy link
Contributor

This seems like it's worth fixing. I will take it on.

@catamorphism
Copy link
Contributor

Blocked on #3993

@catamorphism
Copy link
Contributor

#3993 is fixed so I should be able to fix this now.

catamorphism added a commit to catamorphism/rust that referenced this issue Nov 30, 2012
If an enum type's only variant is private, disallow dereferencing
values of its type, as per rust-lang#818.

Due to rust-lang#4082, this only applies to enums that are in the same crate.
@catamorphism
Copy link
Contributor

Done, pending review: #4084

keeperofdakeys pushed a commit to keeperofdakeys/rust that referenced this issue Dec 12, 2017
ZuseZ4 pushed a commit to EnzymeAD/rust that referenced this issue Mar 7, 2023
* Fix LLVM 15

* Multi version tests

* Fix build on 15+
celinval added a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

3 participants