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

[experiment] Introduce a reliable way to refer to the standard library and use it for calling panic #62002

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions src/libcore/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ macro_rules! assert_eq {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
#[cfg(bootstrap)]
panic!(r#"assertion failed: `(left == right)`
left: `{:?}`,
right: `{:?}`"#, &*left_val, &*right_val);
#[cfg(not(bootstrap))]
__rustc_standard_library::panic!(r#"assertion failed: `(left == right)`
left: `{:?}`,
right: `{:?}`"#, &*left_val, &*right_val)
}
}
Expand All @@ -67,8 +72,14 @@ macro_rules! assert_eq {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
#[cfg(bootstrap)]
panic!(r#"assertion failed: `(left == right)`
left: `{:?}`,
right: `{:?}`: {}"#, &*left_val, &*right_val,
format_args!($($arg)+));
#[cfg(not(bootstrap))]
__rustc_standard_library::panic!(r#"assertion failed: `(left == right)`
left: `{:?}`,
right: `{:?}`: {}"#, &*left_val, &*right_val,
format_args!($($arg)+))
}
Expand Down Expand Up @@ -107,8 +118,13 @@ macro_rules! assert_ne {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
#[cfg(bootstrap)]
panic!(r#"assertion failed: `(left != right)`
left: `{:?}`,
right: `{:?}`"#, &*left_val, &*right_val);
#[cfg(not(bootstrap))]
__rustc_standard_library::panic!(r#"assertion failed: `(left != right)`
left: `{:?}`,
right: `{:?}`"#, &*left_val, &*right_val)
}
}
Expand All @@ -124,8 +140,14 @@ macro_rules! assert_ne {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
#[cfg(bootstrap)]
panic!(r#"assertion failed: `(left != right)`
left: `{:?}`,
right: `{:?}`: {}"#, &*left_val, &*right_val,
format_args!($($arg)+));
#[cfg(not(bootstrap))]
__rustc_standard_library::panic!(r#"assertion failed: `(left != right)`
left: `{:?}`,
right: `{:?}`: {}"#, &*left_val, &*right_val,
format_args!($($arg)+))
}
Expand Down Expand Up @@ -491,7 +513,10 @@ macro_rules! writeln {
#[stable(feature = "rust1", since = "1.0.0")]
macro_rules! unreachable {
() => ({
panic!("internal error: entered unreachable code")
#[cfg(bootstrap)]
panic!("internal error: entered unreachable code");
#[cfg(not(bootstrap))]
__rustc_standard_library::panic!("internal error: entered unreachable code")
});
($msg:expr) => ({
$crate::unreachable!("{}", $msg)
Expand All @@ -500,7 +525,12 @@ macro_rules! unreachable {
$crate::unreachable!($msg)
});
($fmt:expr, $($arg:tt)*) => ({
panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
#[cfg(bootstrap)]
panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*);
#[cfg(not(bootstrap))]
__rustc_standard_library::panic!(
concat!("internal error: entered unreachable code: ", $fmt), $($arg)*
)
});
}

Expand Down Expand Up @@ -554,6 +584,16 @@ macro_rules! unreachable {
/// // we aren't even using baz() yet, so this is fine.
/// }
/// ```
#[cfg(not(bootstrap))]
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
macro_rules! unimplemented {
() => (__rustc_standard_library::panic!("not yet implemented"));
($($arg:tt)+) =>
(__rustc_standard_library::panic!("not yet implemented: {}", format_args!($($arg)*)));
}
/// Bootstrap doc
#[cfg(bootstrap)]
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
macro_rules! unimplemented {
Expand Down Expand Up @@ -613,6 +653,16 @@ macro_rules! unimplemented {
/// // we aren't even using baz() yet, so this is fine.
/// }
/// ```
#[cfg(not(bootstrap))]
#[macro_export]
#[unstable(feature = "todo_macro", issue = "59277")]
macro_rules! todo {
() => (__rustc_standard_library::panic!("not yet implemented"));
($($arg:tt)+) =>
(__rustc_standard_library::panic!("not yet implemented: {}", format_args!($($arg)*)));
}
/// Bootstrap doc
#[cfg(bootstrap)]
#[macro_export]
#[unstable(feature = "todo_macro", issue = "59277")]
macro_rules! todo {
Expand Down
6 changes: 6 additions & 0 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3756,6 +3756,12 @@ impl<'a> Resolver<'a> {
self.resolve_crate_root(ident)));
continue;
}
if name == kw::StdLib {
// `__rustc_standard_library::a::b`
module = Some(ModuleOrUniformRoot::Module(
self.injected_crate.unwrap_or(self.graph_root)));
continue;
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/libsyntax_ext/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use syntax::parse::token::{self, TokenKind};
use syntax::parse::parser::Parser;
use syntax::print::pprust;
use syntax::ptr::P;
use syntax::symbol::{sym, Symbol};
use syntax::symbol::{kw, sym, Symbol};
use syntax::tokenstream::{TokenStream, TokenTree};
use syntax_pos::{Span, DUMMY_SP};

Expand All @@ -27,7 +27,13 @@ pub fn expand_assert<'cx>(

let sp = sp.apply_mark(cx.current_expansion.mark);
let panic_call = Mac_ {
path: Path::from_ident(Ident::new(sym::panic, sp)),
path: Path {
segments: vec![
PathSegment::from_ident(Ident::new(kw::StdLib, sp)),
PathSegment::from_ident(Ident::new(sym::panic, sp)),
],
span: sp,
},
tts: custom_message.unwrap_or_else(|| {
TokenStream::from(TokenTree::token(
TokenKind::lit(token::Str, Symbol::intern(&format!(
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ symbols! {
PathRoot: "{{root}}",
DollarCrate: "$crate",
Underscore: "_",
StdLib: "__rustc_standard_library",

// Keywords that are used in stable Rust.
As: "as",
Expand Down Expand Up @@ -1043,7 +1044,8 @@ impl Symbol {
self == kw::SelfUpper ||
self == kw::Crate ||
self == kw::PathRoot ||
self == kw::DollarCrate
self == kw::DollarCrate ||
self == kw::StdLib
}

/// This symbol can be a raw identifier.
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/hygiene/no_implicit_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ mod bar {
}
fn f() {
::foo::m!();
assert_eq!(0, 0); //~ ERROR cannot find macro `panic!` in this scope
assert!(true); // OK
assert_eq!(0, 0); // OK
}
}

Expand Down
11 changes: 1 addition & 10 deletions src/test/ui/hygiene/no_implicit_prelude.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ LL | fn f() { ::bar::m!(); }
LL | Vec::new();
| ^^^ use of undeclared type or module `Vec`

error: cannot find macro `panic!` in this scope
--> $DIR/no_implicit_prelude.rs:16:9
|
LL | assert_eq!(0, 0);
| ^^^^^^^^^^^^^^^^^
|
= help: have you added the `#[macro_use]` on the module/import?
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0599]: no method named `clone` found for type `()` in the current scope
--> $DIR/no_implicit_prelude.rs:12:12
|
Expand All @@ -29,7 +20,7 @@ LL | ().clone()
= note: the following trait is implemented but not in scope, perhaps add a `use` for it:
`use std::clone::Clone;`

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0433, E0599.
For more information about an error, try `rustc --explain E0433`.