rust-lang / rust-lang/rust

`unnameable_types` should show where the type is reachable

Open
#126,435 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-lints A-visibility D-terse L-unnameable_types T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
// This is just the documentation sample for the lint
#![deny(unnameable_types)]

mod m {
    pub struct S;
}

pub fn get_unnameable() -> m::S { m::S }
Current output
error: struct `S` is reachable but cannot be named
 --> src/lib.rs:5:5
  |
5 |     pub struct S;
  |     ^^^^^^^^^^^^ reachable at visibility `pub`, but can only be named at visibility `pub(crate)`
  |
note: the lint level is defined here
 --> src/lib.rs:2:9
  |
2 | #![deny(unnameable_types)]
  |         ^^^^^^^^^^^^^^^^
Desired output
error: struct `S` is reachable but cannot be named
 --> src/lib.rs:5:5
  |
5 |     pub struct S;
  |     ^^^^^^^^^^^^ reachable at visibility `pub`, but can only be named at visibility `pub(crate)`
  |
note: `S` is reachable from here
  |
8 | pub fn get_unnameable() -> m::S { m::S }
  |                            ^^^^ returns a value of type `S`
  |
note: the lint level is defined here
 --> src/lib.rs:2:9
  |
2 | #![deny(unnameable_types)]
  |         ^^^^^^^^^^^^^^^^
Rationale and extra context

The output should give at least one example of how the type is reachable. This will:

  • Help understand why the lint is not a false-positive (a reaction I had when first enabling it — “surely these can't be…”).
  • If the desired outcome is that the type is not reachable, help the user make it not reachable.

Right now, this is a pure “hidden action at a distance” situation: there is no way to discover where the type is reachable other than by searching for uses of it (which can be difficult).

Other cases

No response

Rust Version
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-apple-darwin
release: 1.79.0
LLVM version: 18.1.7
Anything else?

@rustbot label +A-lint +A-visibility +L-unnameable_types

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

Locate the compiler implementation and tests for the unnameable_types lint, then reproduce the issue with the provided Rust sample. Trace how the lint identifies reachable types and add a diagnostic example showing where S is reachable; done means the output includes that note while retaining the existing lint and level diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.