Skip to content

Commit bd8d030

Browse files
committed
Auto merge of #54338 - orium:fix-macro-inc-comp, r=nrc
Use full name to identify a macro in a `FileName`. Before this two macros with same name would be indistinguishable inside a `FileName`. This caused a bug in incremental compilation (see #53097) since two different macros would map out to the same `StableFilemapId`. Fixes #53097. r? @nrc
2 parents c222479 + 2d7edf9 commit bd8d030

File tree

11 files changed

+47
-15
lines changed

11 files changed

+47
-15
lines changed

Diff for: src/librustc/hir/map/definitions.rs

+25
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,31 @@ impl DefPath {
287287
s
288288
}
289289

290+
/// Return filename friendly string of the DefPah with the
291+
/// crate-prefix.
292+
pub fn to_string_friendly<F>(&self, crate_imported_name: F) -> String
293+
where F: FnOnce(CrateNum) -> Symbol
294+
{
295+
let crate_name_str = crate_imported_name(self.krate).as_str();
296+
let mut s = String::with_capacity(crate_name_str.len() + self.data.len() * 16);
297+
298+
write!(s, "::{}", crate_name_str).unwrap();
299+
300+
for component in &self.data {
301+
if component.disambiguator == 0 {
302+
write!(s, "::{}", component.data.as_interned_str()).unwrap();
303+
} else {
304+
write!(s,
305+
"{}[{}]",
306+
component.data.as_interned_str(),
307+
component.disambiguator)
308+
.unwrap();
309+
}
310+
}
311+
312+
s
313+
}
314+
290315
/// Return filename friendly string of the DefPah without
291316
/// the crate-prefix. This method is useful if you don't have
292317
/// a TyCtxt available.

Diff for: src/librustc/ty/query/on_disk_cache.rs

+1
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ impl<'a, 'tcx, 'x> SpecializedDecoder<interpret::AllocId> for CacheDecoder<'a, '
606606
alloc_decoding_session.decode_alloc_id(self)
607607
}
608608
}
609+
609610
impl<'a, 'tcx, 'x> SpecializedDecoder<Span> for CacheDecoder<'a, 'tcx, 'x> {
610611
fn specialized_decode(&mut self) -> Result<Span, Self::Error> {
611612
let tag: u8 = Decodable::decode(self)?;

Diff for: src/librustc_metadata/creader.rs

+1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ impl<'a> CrateLoader<'a> {
256256

257257
let cmeta = cstore::CrateMetadata {
258258
name: crate_root.name,
259+
imported_name: ident,
259260
extern_crate: Lock::new(None),
260261
def_path_table: Lrc::new(def_path_table),
261262
trait_impls,

Diff for: src/librustc_metadata/cstore.rs

+5
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ pub struct ImportedSourceFile {
5353
}
5454

5555
pub struct CrateMetadata {
56+
/// Original name of the crate.
5657
pub name: Symbol,
5758

59+
/// Name of the crate as imported. I.e. if imported with
60+
/// `extern crate foo as bar;` this will be `bar`.
61+
pub imported_name: Symbol,
62+
5863
/// Information about the extern crate that caused this crate to
5964
/// be loaded. If this is `None`, then the crate was injected
6065
/// (e.g., by the allocator)

Diff for: src/librustc_metadata/cstore_impl.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,7 @@ impl cstore::CStore {
441441
let data = self.get_crate_data(id.krate);
442442
if let Some(ref proc_macros) = data.proc_macros {
443443
return LoadedMacro::ProcMacro(proc_macros[id.index.to_proc_macro_index()].1.clone());
444-
} else if data.name == "proc_macro" &&
445-
self.get_crate_data(id.krate).item_name(id.index) == "quote" {
444+
} else if data.name == "proc_macro" && data.item_name(id.index) == "quote" {
446445
use syntax::ext::base::SyntaxExtension;
447446
use syntax_ext::proc_macro_impl::BangProcMacro;
448447

@@ -454,8 +453,9 @@ impl cstore::CStore {
454453
return LoadedMacro::ProcMacro(Lrc::new(ext));
455454
}
456455

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

460460
let source_file = sess.parse_sess.source_map().new_source_file(source_name, def.body);
461461
let local_span = Span::new(source_file.start_pos, source_file.end_pos, NO_EXPANSION);

Diff for: src/librustc_metadata/decoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,10 @@ impl<'a, 'tcx> CrateMetadata {
11061106
}
11071107
}
11081108

1109-
pub fn get_macro(&self, id: DefIndex) -> (InternedString, MacroDef) {
1109+
pub fn get_macro(&self, id: DefIndex) -> MacroDef {
11101110
let entry = self.entry(id);
11111111
match entry.kind {
1112-
EntryKind::MacroDef(macro_def) => (self.item_name(id), macro_def.decode(self)),
1112+
EntryKind::MacroDef(macro_def) => macro_def.decode(self),
11131113
_ => bug!(),
11141114
}
11151115
}

Diff for: src/libsyntax_pos/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ scoped_thread_local!(pub static GLOBALS: Globals);
8787
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash, RustcDecodable, RustcEncodable)]
8888
pub enum FileName {
8989
Real(PathBuf),
90-
/// e.g. "std" macros
90+
/// A macro. This includes the full name of the macro, so that there are no clashes.
9191
Macros(String),
9292
/// call to `quote!`
9393
QuoteExpansion,

Diff for: src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ LL | r#async = consumes_async_raw!(async); //~ ERROR no rules expected the t
2323
| ^^^^^
2424

2525
error: expected one of `move`, `|`, or `||`, found `<eof>`
26-
--> <passes_ident macros>:1:22
26+
--> <::edition_kw_macro_2015::passes_ident macros>:1:22
2727
|
2828
LL | ( $ i : ident ) => ( $ i )
2929
| ^^^ expected one of `move`, `|`, or `||` here

Diff for: src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ LL | r#async = consumes_async_raw!(async); //~ ERROR no rules expected the t
2323
| ^^^^^
2424

2525
error: expected one of `move`, `|`, or `||`, found `<eof>`
26-
--> <passes_ident macros>:1:22
26+
--> <::edition_kw_macro_2018::passes_ident macros>:1:22
2727
|
2828
LL | ( $ i : ident ) => ( $ i )
2929
| ^^^ expected one of `move`, `|`, or `||` here

Diff for: src/test/ui/imports/local-modularized-tricky-fail-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ LL | define_panic!();
6060
= note: macro-expanded macros do not shadow
6161

6262
error[E0659]: `panic` is ambiguous
63-
--> <panic macros>:1:13
63+
--> <::std::macros::panic macros>:1:13
6464
|
6565
LL | ( ) => ( { panic ! ( "explicit panic" ) } ) ; ( $ msg : expr ) => (
6666
| ^^^^^ ambiguous name

Diff for: src/test/ui/macro_backtrace/main.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ LL | | }
2222
LL | ping!();
2323
| -------- in this macro invocation
2424
|
25-
::: <ping macros>:1:1
25+
::: <::ping::ping macros>:1:1
2626
|
2727
LL | ( ) => { pong ! ( ) ; }
2828
| -------------------------
@@ -42,31 +42,31 @@ LL | | }
4242
LL | deep!();
4343
| -------- in this macro invocation (#1)
4444
|
45-
::: <deep macros>:1:1
45+
::: <::ping::deep macros>:1:1
4646
|
4747
LL | ( ) => { foo ! ( ) ; }
4848
| ------------------------
4949
| | |
5050
| | in this macro invocation (#2)
5151
| in this expansion of `deep!` (#1)
5252
|
53-
::: <foo macros>:1:1
53+
::: <::ping::foo macros>:1:1
5454
|
5555
LL | ( ) => { bar ! ( ) ; }
5656
| ------------------------
5757
| | |
5858
| | in this macro invocation (#3)
5959
| in this expansion of `foo!` (#2)
6060
|
61-
::: <bar macros>:1:1
61+
::: <::ping::bar macros>:1:1
6262
|
6363
LL | ( ) => { ping ! ( ) ; }
6464
| -------------------------
6565
| | |
6666
| | in this macro invocation (#4)
6767
| in this expansion of `bar!` (#3)
6868
|
69-
::: <ping macros>:1:1
69+
::: <::ping::ping macros>:1:1
7070
|
7171
LL | ( ) => { pong ! ( ) ; }
7272
| -------------------------

0 commit comments

Comments
 (0)