Skip to content

Clarify public type aliasing behavior #26953

@jethrogb

Description

@jethrogb

Compile:

// Example #1
mod mod_a {
    mod sub_a {
        pub struct InnerA;
    }
    pub type A = sub_a::InnerA;
}

use mod_a::A;

fn main() {
    let a=A;
}
error[E0425]: unresolved name `A`
  --> test.rs:12:11
   |
12 |     let a=A;
   |           ^ unresolved name

Yet A is quite obviously imported. The same error appears in a different place when compiling:

// Example #2
mod mod_a {
    mod sub_a {
        pub struct InnerA;
    }
    pub type A = sub_a::InnerA;

    pub fn make_a() -> A {
        A
    }
}

use mod_a::{A,make_a};

fn main() {
    let a: A=make_a();
}
error[E0425]: unresolved name `A`
 --> test.rs:9:9
  |
9 |         A
  |         ^ unresolved name

This compiles:

// Example #3
mod mod_a {
    mod sub_a {
        pub struct InnerA;
    }
    pub type A = sub_a::InnerA;

    pub fn make_a() -> A {
        sub_a::InnerA
    }
}

use mod_a::{A,make_a};

fn main() {
    let a: A=make_a();
}

But wait, I thought in example no. 1, we couldn't resolve A from main()?

Using pub use instead makes example no. 1 work:

// Example #4
mod mod_a {
    mod sub_a {
        pub struct InnerA;
    }
    pub use self::sub_a::InnerA as A;
}

use mod_a::A;

fn main() {
    let a=A;
}

For completeness, here is the tuple-struct version:

// Example #5
mod mod_a {
    mod sub_a {
        pub struct InnerA(pub u8);
    }
    pub type A = sub_a::InnerA;
}

use mod_a::A;

fn main() {
    let a=A(0);
}
error[E0425]: unresolved name `A`
  --> test.rs:12:11
   |
12 |     let a=A(0);
   |           ^ unresolved name

And the regular struct version (compiles without errors):

// Example #6
mod mod_a {
    mod sub_a {
        pub struct InnerA {pub i:u8}
    }
    pub type A = sub_a::InnerA;
}

use mod_a::A;

fn main() {
    let a=A{i:0};
}

It seems like structure expressionsconstructors are handled differently from other uses of types with regards to aliases. Also, the phrase pub type does not appear in the Rust Book or the Rust Reference.

I propose the following:

  • Improve the error message in cases 1, 2 and 5 above
  • Improve documentation on public type aliases
  • Improve documentation on structure expressionsconstructors involving type aliases

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsP-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions