Skip to content

Commit a0db04b

Browse files
committed
Auto merge of #44940 - philipc:remap-path, r=michaelwoerister
Don't use remapped path when loading modules and include files Fixes bug reported in #41555 (comment). cc @michaelwoerister
2 parents bd36dcf + 9bbd7a3 commit a0db04b

File tree

10 files changed

+59
-12
lines changed

10 files changed

+59
-12
lines changed

src/librustc/ich/impls_syntax.rs

+1
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for FileMap {
354354
let FileMap {
355355
ref name,
356356
name_was_remapped,
357+
unmapped_path: _,
357358
crate_of_origin,
358359
// Do not hash the source as it is not encoded
359360
src: _,

src/librustc/session/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ pub struct Session {
6868
pub derive_registrar_fn: Cell<Option<ast::NodeId>>,
6969
pub default_sysroot: Option<PathBuf>,
7070
// The name of the root source file of the crate, in the local file system.
71-
// The path is always expected to be absolute. `None` means that there is no
72-
// source file.
71+
// `None` means that there is no source file.
7372
pub local_crate_source_file: Option<String>,
7473
// The directory the compiler has been executed in plus a flag indicating
7574
// if the value stored here has been affected by path remapping.
@@ -722,7 +721,6 @@ pub fn build_session_(sopts: config::Options,
722721

723722
let file_path_mapping = sopts.file_path_mapping();
724723

725-
// Make the path absolute, if necessary
726724
let local_crate_source_file = local_crate_source_file.map(|path| {
727725
file_path_mapping.map_prefix(path.to_string_lossy().into_owned()).0
728726
});

src/librustdoc/test.rs

+2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ fn run_test(test: &str, cratename: &str, filename: &str, cfgs: Vec<String>, libs
184184
// the test harness wants its own `main` & top level functions, so
185185
// never wrap the test in `fn main() { ... }`
186186
let test = make_test(test, Some(cratename), as_test_harness, opts);
187+
// FIXME(#44940): if doctests ever support path remapping, then this filename
188+
// needs to be the result of CodeMap::span_to_unmapped_path
187189
let input = config::Input::Str {
188190
name: filename.to_owned(),
189191
input: test.to_owned(),

src/libsyntax/codemap.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,21 @@ impl CodeMap {
162162
let start_pos = self.next_start_pos();
163163
let mut files = self.files.borrow_mut();
164164

165+
// The path is used to determine the directory for loading submodules and
166+
// include files, so it must be before remapping.
167+
// Note that filename may not be a valid path, eg it may be `<anon>` etc,
168+
// but this is okay because the directory determined by `path.pop()` will
169+
// be empty, so the working directory will be used.
170+
let unmapped_path = PathBuf::from(filename.clone());
171+
165172
let (filename, was_remapped) = self.path_mapping.map_prefix(filename);
166-
let filemap =
167-
Rc::new(FileMap::new(filename, was_remapped, src, Pos::from_usize(start_pos)));
173+
let filemap = Rc::new(FileMap::new(
174+
filename,
175+
was_remapped,
176+
unmapped_path,
177+
src,
178+
Pos::from_usize(start_pos),
179+
));
168180

169181
files.push(filemap.clone());
170182

@@ -216,6 +228,7 @@ impl CodeMap {
216228
let filemap = Rc::new(FileMap {
217229
name: filename,
218230
name_was_remapped,
231+
unmapped_path: None,
219232
crate_of_origin,
220233
src: None,
221234
src_hash,
@@ -342,7 +355,12 @@ impl CodeMap {
342355
}
343356

344357
pub fn span_to_filename(&self, sp: Span) -> FileName {
345-
self.lookup_char_pos(sp.lo()).file.name.to_string()
358+
self.lookup_char_pos(sp.lo()).file.name.clone()
359+
}
360+
361+
pub fn span_to_unmapped_path(&self, sp: Span) -> PathBuf {
362+
self.lookup_char_pos(sp.lo()).file.unmapped_path.clone()
363+
.expect("CodeMap::span_to_unmapped_path called for imported FileMap?")
346364
}
347365

348366
pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {

src/libsyntax/ext/expand.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use visit::Visitor;
3535

3636
use std::collections::HashMap;
3737
use std::mem;
38-
use std::path::PathBuf;
3938
use std::rc::Rc;
4039

4140
macro_rules! expansions {
@@ -200,7 +199,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
200199
self.cx.crate_root = std_inject::injected_crate_name(&krate);
201200
let mut module = ModuleData {
202201
mod_path: vec![Ident::from_str(&self.cx.ecfg.crate_name)],
203-
directory: PathBuf::from(self.cx.codemap().span_to_filename(krate.span)),
202+
directory: self.cx.codemap().span_to_unmapped_path(krate.span),
204203
};
205204
module.directory.pop();
206205
self.cx.current_expansion.module = Rc::new(module);
@@ -952,8 +951,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
952951
module.directory.push(&*item.ident.name.as_str());
953952
}
954953
} else {
955-
let mut path =
956-
PathBuf::from(self.cx.parse_sess.codemap().span_to_filename(inner));
954+
let mut path = self.cx.parse_sess.codemap().span_to_unmapped_path(inner);
957955
let directory_ownership = match path.file_name().unwrap().to_str() {
958956
Some("mod.rs") => DirectoryOwnership::Owned,
959957
_ => DirectoryOwnership::UnownedViaMod(false),

src/libsyntax/ext/source_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ fn res_rel_file(cx: &mut ExtCtxt, sp: syntax_pos::Span, arg: &Path) -> PathBuf {
197197
// after macro expansion (that is, they are unhygienic).
198198
if !arg.is_absolute() {
199199
let callsite = sp.source_callsite();
200-
let mut path = PathBuf::from(&cx.codemap().span_to_filename(callsite));
200+
let mut path = cx.codemap().span_to_unmapped_path(callsite);
201201
path.pop();
202202
path.push(arg);
203203
path

src/libsyntax/parse/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl<'a> Parser<'a> {
525525
if let Some(directory) = directory {
526526
parser.directory = directory;
527527
} else if parser.span != syntax_pos::DUMMY_SP {
528-
parser.directory.path = PathBuf::from(sess.codemap().span_to_filename(parser.span));
528+
parser.directory.path = sess.codemap().span_to_unmapped_path(parser.span);
529529
parser.directory.path.pop();
530530
}
531531

src/libsyntax_pos/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use std::cmp::{self, Ordering};
3232
use std::fmt;
3333
use std::hash::Hasher;
3434
use std::ops::{Add, Sub};
35+
use std::path::PathBuf;
3536
use std::rc::Rc;
3637

3738
use rustc_data_structures::stable_hasher::StableHasher;
@@ -501,6 +502,9 @@ pub struct FileMap {
501502
pub name: FileName,
502503
/// True if the `name` field above has been modified by -Zremap-path-prefix
503504
pub name_was_remapped: bool,
505+
/// The unmapped path of the file that the source came from.
506+
/// Set to `None` if the FileMap was imported from an external crate.
507+
pub unmapped_path: Option<PathBuf>,
504508
/// Indicates which crate this FileMap was imported from.
505509
pub crate_of_origin: u32,
506510
/// The complete source code
@@ -626,6 +630,7 @@ impl Decodable for FileMap {
626630
Ok(FileMap {
627631
name,
628632
name_was_remapped,
633+
unmapped_path: None,
629634
// `crate_of_origin` has to be set by the importer.
630635
// This value matches up with rustc::hir::def_id::INVALID_CRATE.
631636
// That constant is not available here unfortunately :(
@@ -651,6 +656,7 @@ impl fmt::Debug for FileMap {
651656
impl FileMap {
652657
pub fn new(name: FileName,
653658
name_was_remapped: bool,
659+
unmapped_path: PathBuf,
654660
mut src: String,
655661
start_pos: BytePos) -> FileMap {
656662
remove_bom(&mut src);
@@ -664,6 +670,7 @@ impl FileMap {
664670
FileMap {
665671
name,
666672
name_was_remapped,
673+
unmapped_path: Some(unmapped_path),
667674
crate_of_origin: 0,
668675
src: Some(Rc::new(src)),
669676
src_hash,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-test: this is not a test
12+
13+
#[inline]
14+
pub fn some_aux_mod_function() -> i32 {
15+
1234
16+
}

src/test/codegen/remap_path_prefix/main.rs

+7
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@
1616

1717
extern crate remap_path_prefix_aux;
1818

19+
// Here we check that submodules and include files are found using the path without
20+
// remapping. This test requires that rustc is called with an absolute path.
21+
mod aux_mod;
22+
include!("aux_mod.rs");
23+
1924
// Here we check that the expansion of the file!() macro is mapped.
2025
// CHECK: internal constant [34 x i8] c"/the/src/remap_path_prefix/main.rs"
2126
pub static FILE_PATH: &'static str = file!();
2227

2328
fn main() {
2429
remap_path_prefix_aux::some_aux_function();
30+
aux_mod::some_aux_mod_function();
31+
some_aux_mod_function();
2532
}
2633

2734
// Here we check that local debuginfo is mapped correctly.

0 commit comments

Comments
 (0)