Skip to content

Rustdoc should ignore unresolved imports in function bodies #97976

Closed as not planned
@SpriteOvO

Description

@SpriteOvO

I tried this code:

Cargo.toml

[package]
name = "issue-demo"
version = "0.1.0"
edition = "2021"

[dependencies]

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["debugapi"], optional = true }

src/main.rs

#[cfg(any(all(windows, feature = "winapi"), doc))]
fn doc_me() {
    use winapi::um::debugapi::OutputDebugStringW;

    let text = vec![0_u16];
    let text = text.as_ptr();
    unsafe { OutputDebugStringW(text) }
}

fn main() {
    println!("Hello, world!");
}

Run

cargo doc --all-features --open
# or
cargo +nightly doc --all-features --open

on platforms other than Windows.


I expected to see this happen: No errors in the terminal, the doc_me function should be displayed in the documentation page. (According to the Rustdoc documentation Interactions between platform-specific docs)

Instead, this happened: Terminal outputs error:

$ cargo +nightly doc --all-features
 Documenting issue-demo v0.1.0 (REDACTED/issue-proj)
error[E0433]: failed to resolve: use of undeclared crate or module `winapi`
 --> src/main.rs:3:9
  |
3 |     use winapi::um::debugapi::OutputDebugStringW;
  |         ^^^^^^ use of undeclared crate or module `winapi`

error: Compilation failed, aborting rustdoc

For more information about this error, try `rustc --explain E0433`.
error: could not document `issue-demo`

Caused by:
  process didn't exit successfully: `rustdoc --edition=2021 --crate-type bin --crate-name issue_demo src/main.rs -o REDACTED/issue-proj/target/doc --cfg 'feature="winapi"' --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --document-private-items '-Arustdoc::private-intra-doc-links' -C metadata=9a8ace04a496849a -L dependency=REDACTED/issue-proj/target/debug/deps --crate-version 0.1.0` (exit status: 1)

If we change the code of src/main.rs to: (remove the use statement)

#[cfg(any(all(windows, feature = "winapi"), doc))]
fn doc_me() {
    let text = vec![0_u16];
    let text = text.as_ptr();
    unsafe { winapi::um::debugapi::OutputDebugStringW(text) }
}

fn main() {
    println!("Hello, world!");
}

It will work as expected.


Meta

rustc --version --verbose:

rustc 1.61.0 (fe5b13d68 2022-05-18)
binary: rustc
commit-hash: fe5b13d681f25ee6474be29d748c65adcd91f69e
commit-date: 2022-05-18
host: x86_64-unknown-linux-gnu
release: 1.61.0
LLVM version: 14.0.0

rustc +nightly --version --verbose:

rustc 1.63.0-nightly (420c970cb 2022-06-09)
binary: rustc
commit-hash: 420c970cb1edccbf60ff2aeb51ca01e2300b09ef
commit-date: 2022-06-09
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions