rust-lang / rust-lang/rust

rustc ignores first exposed private type / span incorrect

Open
#127,124 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried to look for an existing issue but could not find any. Apologies if this has been reported already.

It should be reporting the private error for Wants_X, but instead it completely skips the error on builder() and displays error for finish()

Code
mod something {
    pub struct S {
        x: u32,
    }
    impl S {
        pub fn builder() -> Wants_X {
            Wants_X
        }
    }
    struct Wants_X;
    pub struct Data_X(u32);
    impl Wants_X {
        pub fn next_number(self, number: u32) -> Wants_Finish {
            Wants_Finish(Data_X(number))
        }
    }
    struct Wants_Finish(Data_X);
    impl Wants_Finish {
        pub fn finish(self) -> S {
            S {
                x: self.0.0
            }
        }
    }
}

fn main() {
    {
        use something::*;
        let s = S::builder()
            .next_number(32)
            .finish();
    }
}
Current output
error: type `Wants_Finish` is private
  --> src\bin\test_private.rs:32:14
   |
32 |             .finish();
   |              ^^^^^^ private type

If you remove the finish() line, the output is still wrong (span appears to be incorrect).

error: type `Wants_Finish` is private
  --> src/main.rs:30:17
   |
30 |           let s = S::builder()
   |  _________________^
31 | |             .next_number(32);
   | |____________________________^ private type

If you make Wants_Finish pub, you get this (span definitely wrong)

error: type `Wants_X` is private
  --> src/main.rs:31:14
   |
31 |             .next_number(32)
   |              ^^^^^^^^^^^ private type
Desired output
error: type `Wants_X` is private
  --> src/main.rs:30:17
   |
30 |         let s = S::builder()
   |                 ^^^^^^^^^^^^ private type
Rationale and extra context

No response

Other cases

No response

Rust Version
1.81.0-nightly
(2024-06-24 6b0f4b5ec3aa707ecaa7)
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported diagnostics with the Rust code in the issue using the noted nightly version, comparing the spans for builder(), next_number(), and finish(). Then trace rustc's private-type error reporting and span selection for chained method calls. Done means the first exposed private type is reported at the builder() return and the diagnostic span matches the intended expression.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.