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

Use full name to identify a macro in a FileName. #54338

Merged
merged 1 commit into from
Sep 28, 2018
Merged
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
25 changes: 25 additions & 0 deletions src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,31 @@ impl DefPath {
s
}

/// Return filename friendly string of the DefPah with the
/// crate-prefix.
pub fn to_string_friendly<F>(&self, crate_imported_name: F) -> String
where F: FnOnce(CrateNum) -> Symbol
{
let crate_name_str = crate_imported_name(self.krate).as_str();
let mut s = String::with_capacity(crate_name_str.len() + self.data.len() * 16);

write!(s, "::{}", crate_name_str).unwrap();

for component in &self.data {
if component.disambiguator == 0 {
write!(s, "::{}", component.data.as_interned_str()).unwrap();
} else {
write!(s,
"{}[{}]",
component.data.as_interned_str(),
component.disambiguator)
.unwrap();
}
}

s
}

/// Return filename friendly string of the DefPah without
/// the crate-prefix. This method is useful if you don't have
/// a TyCtxt available.
Expand Down
1 change: 1 addition & 0 deletions src/librustc/ty/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ impl<'a, 'tcx, 'x> SpecializedDecoder<interpret::AllocId> for CacheDecoder<'a, '
alloc_decoding_session.decode_alloc_id(self)
}
}

impl<'a, 'tcx, 'x> SpecializedDecoder<Span> for CacheDecoder<'a, 'tcx, 'x> {
fn specialized_decode(&mut self) -> Result<Span, Self::Error> {
let tag: u8 = Decodable::decode(self)?;
Expand Down
1 change: 1 addition & 0 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ impl<'a> CrateLoader<'a> {

let cmeta = cstore::CrateMetadata {
name: crate_root.name,
imported_name: ident,
extern_crate: Lock::new(None),
def_path_table: Lrc::new(def_path_table),
trait_impls,
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_metadata/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ pub struct ImportedSourceFile {
}

pub struct CrateMetadata {
/// Original name of the crate.
pub name: Symbol,

/// Name of the crate as imported. I.e. if imported with
/// `extern crate foo as bar;` this will be `bar`.
pub imported_name: Symbol,

/// Information about the extern crate that caused this crate to
/// be loaded. If this is `None`, then the crate was injected
/// (e.g., by the allocator)
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,7 @@ impl cstore::CStore {
let data = self.get_crate_data(id.krate);
if let Some(ref proc_macros) = data.proc_macros {
return LoadedMacro::ProcMacro(proc_macros[id.index.to_proc_macro_index()].1.clone());
} else if data.name == "proc_macro" &&
self.get_crate_data(id.krate).item_name(id.index) == "quote" {
} else if data.name == "proc_macro" && data.item_name(id.index) == "quote" {
use syntax::ext::base::SyntaxExtension;
use syntax_ext::proc_macro_impl::BangProcMacro;

Expand All @@ -460,8 +459,9 @@ impl cstore::CStore {
return LoadedMacro::ProcMacro(Lrc::new(ext));
}

let (name, def) = data.get_macro(id.index);
let source_name = FileName::Macros(name.to_string());
let def = data.get_macro(id.index);
let macro_full_name = data.def_path(id.index).to_string_friendly(|_| data.imported_name);
let source_name = FileName::Macros(macro_full_name);

let source_file = sess.parse_sess.source_map().new_source_file(source_name, def.body);
let local_span = Span::new(source_file.start_pos, source_file.end_pos, NO_EXPANSION);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,10 +1069,10 @@ impl<'a, 'tcx> CrateMetadata {
}
}

pub fn get_macro(&self, id: DefIndex) -> (InternedString, MacroDef) {
pub fn get_macro(&self, id: DefIndex) -> MacroDef {
let entry = self.entry(id);
match entry.kind {
EntryKind::MacroDef(macro_def) => (self.item_name(id), macro_def.decode(self)),
EntryKind::MacroDef(macro_def) => macro_def.decode(self),
_ => bug!(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax_pos/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ scoped_thread_local!(pub static GLOBALS: Globals);
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash, RustcDecodable, RustcEncodable)]
pub enum FileName {
Real(PathBuf),
/// e.g. "std" macros
/// A macro. This includes the full name of the macro, so that there are no clashes.
Macros(String),
/// call to `quote!`
QuoteExpansion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LL | r#async = consumes_async_raw!(async); //~ ERROR no rules expected the t
| ^^^^^

error: expected one of `move`, `|`, or `||`, found `<eof>`
--> <passes_ident macros>:1:22
--> <::edition_kw_macro_2015::passes_ident macros>:1:22
|
LL | ( $ i : ident ) => ( $ i )
| ^^^ expected one of `move`, `|`, or `||` here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LL | r#async = consumes_async_raw!(async); //~ ERROR no rules expected the t
| ^^^^^

error: expected one of `move`, `|`, or `||`, found `<eof>`
--> <passes_ident macros>:1:22
--> <::edition_kw_macro_2018::passes_ident macros>:1:22
|
LL | ( $ i : ident ) => ( $ i )
| ^^^ expected one of `move`, `|`, or `||` here
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/imports/local-modularized-tricky-fail-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ LL | define_panic!();
= note: macro-expanded macros do not shadow

error[E0659]: `panic` is ambiguous
--> <panic macros>:1:13
--> <::std::macros::panic macros>:1:13
|
LL | ( ) => ( { panic ! ( "explicit panic" ) } ) ; ( $ msg : expr ) => (
| ^^^^^ ambiguous name
Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/macro_backtrace/main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ LL | | }
LL | ping!();
| -------- in this macro invocation
|
::: <ping macros>:1:1
::: <::ping::ping macros>:1:1
|
LL | ( ) => { pong ! ( ) ; }
| -------------------------
Expand All @@ -42,31 +42,31 @@ LL | | }
LL | deep!();
| -------- in this macro invocation (#1)
|
::: <deep macros>:1:1
::: <::ping::deep macros>:1:1
|
LL | ( ) => { foo ! ( ) ; }
| ------------------------
| | |
| | in this macro invocation (#2)
| in this expansion of `deep!` (#1)
|
::: <foo macros>:1:1
::: <::ping::foo macros>:1:1
|
LL | ( ) => { bar ! ( ) ; }
| ------------------------
| | |
| | in this macro invocation (#3)
| in this expansion of `foo!` (#2)
|
::: <bar macros>:1:1
::: <::ping::bar macros>:1:1
|
LL | ( ) => { ping ! ( ) ; }
| -------------------------
| | |
| | in this macro invocation (#4)
| in this expansion of `bar!` (#3)
|
::: <ping macros>:1:1
::: <::ping::ping macros>:1:1
|
LL | ( ) => { pong ! ( ) ; }
| -------------------------
Expand Down