Skip to content

Restrict sysroot crate imports to those defined in this repo. #143548

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Diggsey
Copy link
Contributor

@Diggsey Diggsey commented Jul 6, 2025

It's common to import dependencies from the sysroot via extern crate rather than use an explicit cargo dependency, when it's necessary to use the same dependency version as used by rustc itself. However, this is dangerous for crates.io crates, since rustc may not pull in the dependency on some targets, or may pull in multiple versions. In both cases, the extern crate fails to resolve.

To address this, re-export all such dependencies from the appropriate rustc_* crates, and use this alias from crates which would otherwise need to use extern crate.

See #143492 for an example of the kind of issue that can occur.

@rustbot
Copy link
Collaborator

rustbot commented Jul 6, 2025

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-compiletest Area: The compiletest test runner A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-rustdoc-json Area: Rustdoc JSON backend A-testsuite Area: The testsuite used to check the correctness of rustc A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jul 6, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 6, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in src/tools/compiletest

cc @jieyouxu

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

Some changes occurred in compiler/rustc_attr_data_structures

cc @jdonszelmann

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

The Miri subtree was changed

cc @rust-lang/miri

Some changes occurred in exhaustiveness checking

cc @Nadrieril

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

rustc_error_messages was changed

cc @davidtwco, @compiler-errors, @TaKO8Ki

@Diggsey Diggsey force-pushed the db-limit-extern-crate-usage branch from 8e8dd82 to c613bce Compare July 6, 2025 22:54
@rustbot
Copy link
Collaborator

rustbot commented Jul 6, 2025

The Miri subtree was changed

cc @rust-lang/miri

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Miri changes LGTM, except the ones in the vendored file where only the header at the top should be edited.

I'm fine with having these Miri changes here, seems easier to do this centrally than coordinate half a dozen PRs.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 7, 2025

Do you think it is feasible to add an internal lint that rejects extern crate items where the crate name doesn't start with rustc_ or sth like that?

@Diggsey
Copy link
Contributor Author

Diggsey commented Jul 7, 2025

@oli-obk I'll look into this. How does testing work for sub-trees? Would each sub-tree need its own copy of the lint, or do the subtree repos have some way to run rust-wide lints?

@RalfJung
Copy link
Member

RalfJung commented Jul 7, 2025

"internal" lints live in rustc like normal lints, but are gated behind a nightly-only flag. Miri enables them in their CI, I don't know if ./x check miri also enables them.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 7, 2025

We have a list of internal lints that rustc has and enables everywhere. We'd have to limit it to crates with #![allow(internal_features)] or sth like it. Most other lints are obviously limited to that as they look for e.g. TyKind .

@RalfJung
Copy link
Member

RalfJung commented Jul 7, 2025

Not sure what you mean about restricting it, the lints need -Zunstable-options -Wrustc::internal to be enabled, right?

@oli-obk
Copy link
Contributor

oli-obk commented Jul 7, 2025

Ah right, my bad

@Diggsey Diggsey force-pushed the db-limit-extern-crate-usage branch 3 times, most recently from 6dd804b to 8561f09 Compare July 12, 2025 21:38
@rust-log-analyzer

This comment has been minimized.

@Diggsey Diggsey force-pushed the db-limit-extern-crate-usage branch from 8561f09 to 2235afc Compare July 12, 2025 22:22
@rust-log-analyzer

This comment has been minimized.

@Diggsey Diggsey force-pushed the db-limit-extern-crate-usage branch from 2235afc to 8dd8124 Compare July 12, 2025 22:54
@rust-log-analyzer

This comment has been minimized.

@Diggsey Diggsey force-pushed the db-limit-extern-crate-usage branch from 8dd8124 to a79cac5 Compare July 12, 2025 23:34
@rust-log-analyzer

This comment has been minimized.

It's common to import dependencies from the sysroot via `extern crate`
rather than use an explicit cargo dependency, when it's necessary to use
the same dependency version as used by rustc itself. However, this is
dangerous for crates.io crates, since rustc may not pull in the
dependency on some targets, or may pull in multiple versions. In both
cases, the `extern crate` fails to resolve.

To address this, re-export all such dependencies from the appropriate
`rustc_*` crates, and use this alias from crates which would otherwise
need to use `extern crate`.
@Diggsey Diggsey force-pushed the db-limit-extern-crate-usage branch from a79cac5 to 12fe444 Compare July 13, 2025 01:18
@rust-log-analyzer
Copy link
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
1 file would be reformatted, 27 files already formatted
--- /checkout/src/bootstrap/bootstrap.py
+++ /checkout/src/bootstrap/bootstrap.py
@@ -1104,7 +1104,9 @@
             env["RUSTFLAGS"] += " -C linker=" + target_linker
         # When changing this list, also update the corresponding list in `Builder::cargo`
         # in `src/bootstrap/src/core/builder.rs`.
-        env["RUSTFLAGS"] += " -Wrust_2018_idioms -Wunused_lifetimes -Wrustc::dangerous_extern_crate"
+        env["RUSTFLAGS"] += (
+            " -Wrust_2018_idioms -Wunused_lifetimes -Wrustc::dangerous_extern_crate"
+        )
         if self.warnings == "default":
             deny_warnings = self.get_toml("deny-warnings", "rust") != "false"
         else:

rerun tidy with `--extra-checks=py:fmt --bless` to reformat Python code
some tidy checks failed
tidy error: checks with external tool 'ruff' failed
Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:01:34
  local time: Sun Jul 13 01:24:35 UTC 2025
  network time: Sun, 13 Jul 2025 01:24:35 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-compiletest Area: The compiletest test runner A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-rustdoc-json Area: Rustdoc JSON backend A-testsuite Area: The testsuite used to check the correctness of rustc A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants