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

Discrepancy between compilation of crates and single-file executables. #15348

Closed
nwin opened this issue Jul 2, 2014 · 5 comments
Closed

Discrepancy between compilation of crates and single-file executables. #15348

nwin opened this issue Jul 2, 2014 · 5 comments

Comments

@nwin
Copy link
Contributor

nwin commented Jul 2, 2014

I have two files:

module.rs

#[main]
fn main() {
    println!("{}", std::str::from_utf8_lossy(b"Hello, world!"));
}

lib.rs

#![crate_id = "testcrate"]
#![crate_type = "bin"]

mod module;

rustc module.rs compiles without any problems. rustc lib.rs yields the error message:

module.rs:3:17: 3:42 error: unresolved name `std::str::from_utf8_lossy`.
module.rs:3     println!("{}", std::str::from_utf8_lossy(b"Hello, world!"));
@nwin nwin changed the title Discrepancy between crates and single-file executables. Discrepancy between compilation of crates and single-file executables. Jul 2, 2014
@alexcrichton
Copy link
Member

The phenomena you're seeing here is that extern crate std is injected at the top of every crate, so the crate root has the name std in scope by default. When you move it to a different module you enter a different namespace, so you'd have to import std again via use std;

@ben0x539
Copy link
Contributor

ben0x539 commented Jul 4, 2014

I sort of think we should always put extern crate symbols in the top-level scope, and only them, and put the crate root into an implicit scope (maybe addressable as ::crate, and injecting use ::crate::* into every module...) below that, so the experience would be consistent between code in the crate root and code in a module, but I guess that gain in consistency might not be worth rejuggling everything and introducing more subtleties, and the impact on intra-crate use items makes it a nonstarter.

@pnkfelix
Copy link
Member

pnkfelix commented Jul 4, 2014

@ben0x539 your suggestion sounds very much to me like rust-lang/rfcs#110 ; can you elaborate on where it differs? Or are they indeed the same idea?

@ben0x539
Copy link
Contributor

ben0x539 commented Jul 4, 2014

@pnkfelix I think I descibred that really badly. I'm basically suggesting that given extern mod foo;, we stop accepting foo::... and require ::foo::... (or use foo; before foo::...), so making all crate items require absolute paths even when referencing them in the crate root. I would also then suggest not allowing extern crate items outside the crate root so they all exist in the outermost scope. I'm basically weirded out by external crates seemingly being nested within the current crate next to any normal item, rather than existing on the same nesting level as the whole of the current crate. The more I think about it, the less this whole plan sounds like a good idea, though.

That wouldn't address the problem that rust-lang/rfcs#110 tries to solve at all, you'd still need a mechanism to ensure that the same crate is referenced by the same path in completely different compilation units.

@steveklabnik
Copy link
Member

This is the kind of item which would require an RFC to change, I'd think. Feel free to persue this over there!

bors added a commit to rust-lang-ci/rust that referenced this issue Aug 7, 2023
Exclude non-identifier aliases from completion filtering text

When building `CompletionItem`s, this excludes aliases that aren't valid identifiers from the "lookup" text used to filter completions in the LSP client. Including them results in weird completion filtering behavior e.g. `Partial>` matching a completion for the `PartialOrd` trait because it has a doc alias of ">".

Closes rust-lang#14692
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants