Skip to content
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

core sometimes is resolved when not defined #112924

Closed
WaffleLapkin opened this issue Jun 22, 2023 · 3 comments
Closed

core sometimes is resolved when not defined #112924

WaffleLapkin opened this issue Jun 22, 2023 · 3 comments
Labels
A-resolve Area: Path resolution C-bug Category: This is a bug.

Comments

@WaffleLapkin
Copy link
Member

WaffleLapkin commented Jun 22, 2023

The following two snippets:

#![crate_type = "lib"]
use core::option::Option;
#![crate_type = "lib"]
const _: core::option::Option<u32> = None;

I expected both of them to either compile, or not.

Instead, only the const one compiles, while the use one doesn't:

error[E0433]: failed to resolve: maybe a missing crate `core`?
 --> ./x.rs:2:5
  |
2 | use core::option::Option;
  |     ^^^^ maybe a missing crate `core`?
  |
  = help: consider adding `extern crate core` to use the `core` crate

Note that the core crate is not defined in either test, as can be seen by using -Zunpretty=hir:

#![crate_type = "lib"]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
use core::option::Option;
#![crate_type = "lib"]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
const _: core::option::Option<()> = None;

Similarly to a const, impl core::iter::Iterator for ... { ... }, fn f() { core::option::Option::Some(()) }, fn f() -> core::option::Option<()> { None } and others work. The only thing I could find to issue an error is a use.

Edition note:

  • non-use
    • On 2015 core::option::Option will work, but both ::core::... and crate::core::... will error
    • On 2018 and 2021 core::option::Option and ::core::... will work, but crate:: will error
  • use
    • On 2015 none of core::..., ::core::... and crate::core work
    • On 2018 and 2021 only core::... and ::core::.... works, while crate::core doesn't

Given all of that it seems that

  1. core is in the extern prelude (as documented), but
  2. core is not present at the crate root, but
  3. Everything that is not a use can access stuff from extern prelude

This is very confusing to say the least.

I'd expect any of the following outcomes:

  1. core is both in the extern prelude and crate root, anything can refer to it (IMO the best option)
  2. core is neither in the extern prelude nor in the crate root, nothing can refer to it

But not what we currently have...

Meta

rustc --version --verbose:

rustc 1.72.0-nightly (065a1f5df 2023-06-21)
binary: rustc
commit-hash: 065a1f5df9c2f1d93269e4d25a2acabbddb0db8d
commit-date: 2023-06-21
host: aarch64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5
@WaffleLapkin WaffleLapkin added A-resolve Area: Path resolution C-bug Category: This is a bug. labels Jun 22, 2023
@petrochenkov
Copy link
Contributor

petrochenkov commented Jun 22, 2023

On 2018+ editions things are exactly as intended by the 2018 import reform - core is in extern prelude, all paths can refer to it regardless of being import or not (that's why the feature was called "uniform paths"), and core is NOT placed into the crate root module.
(And :: means extern prelude.)

On 2015 edition things were simply frozen in whatever state they were before the reform.
(And :: means crate::.)

@petrochenkov
Copy link
Contributor

I tried to change 2015 behavior to be more uniform with modern editions in #57745, but in the end the decision was to just keep legacy things frozen.

@WaffleLapkin
Copy link
Member Author

Oh, I noticed an error in my post, I thought use core... doesn't work on edition >= 2018, but it actually does work. Yeah, then the edition >= 2018 looks sane.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-resolve Area: Path resolution C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants