Skip to content

core sometimes is resolved when not defined #112924

Closed
@WaffleLapkin

Description

@WaffleLapkin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions