Skip to content

Commit c27a82f

Browse files
committed
Don't use remapped path when loading modules and include files
1 parent 46ef620 commit c27a82f

File tree

9 files changed

+49
-11
lines changed

9 files changed

+49
-11
lines changed

src/librustc/ich/impls_syntax.rs

Lines changed: 1 addition & 0 deletions
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+
path: _,
357358
crate_of_origin,
358359
// Do not hash the source as it is not encoded
359360
src: _,

src/librustc/session/mod.rs

Lines changed: 1 addition & 3 deletions
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/libsyntax/codemap.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,16 @@ 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 path = PathBuf::from(filename.clone());
171+
165172
let (filename, was_remapped) = self.path_mapping.map_prefix(filename);
166173
let filemap =
167-
Rc::new(FileMap::new(filename, was_remapped, src, Pos::from_usize(start_pos)));
174+
Rc::new(FileMap::new(filename, was_remapped, path, src, Pos::from_usize(start_pos)));
168175

169176
files.push(filemap.clone());
170177

@@ -216,6 +223,7 @@ impl CodeMap {
216223
let filemap = Rc::new(FileMap {
217224
name: filename,
218225
name_was_remapped,
226+
path: PathBuf::new(),
219227
crate_of_origin,
220228
src: None,
221229
src_hash,
@@ -342,7 +350,11 @@ impl CodeMap {
342350
}
343351

344352
pub fn span_to_filename(&self, sp: Span) -> FileName {
345-
self.lookup_char_pos(sp.lo()).file.name.to_string()
353+
self.lookup_char_pos(sp.lo()).file.name.clone()
354+
}
355+
356+
pub fn span_to_path(&self, sp: Span) -> PathBuf {
357+
self.lookup_char_pos(sp.lo()).file.path.clone()
346358
}
347359

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

src/libsyntax/ext/expand.rs

Lines changed: 2 additions & 4 deletions
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_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_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

Lines changed: 1 addition & 1 deletion
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_path(callsite);
201201
path.pop();
202202
path.push(arg);
203203
path

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 1 deletion
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_path(parser.span);
529529
parser.directory.path.pop();
530530
}
531531

src/libsyntax_pos/lib.rs

Lines changed: 6 additions & 0 deletions
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,8 @@ 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 path of the file that the source came from.
506+
pub path: PathBuf,
504507
/// Indicates which crate this FileMap was imported from.
505508
pub crate_of_origin: u32,
506509
/// The complete source code
@@ -626,6 +629,7 @@ impl Decodable for FileMap {
626629
Ok(FileMap {
627630
name,
628631
name_was_remapped,
632+
path: PathBuf::new(),
629633
// `crate_of_origin` has to be set by the importer.
630634
// This value matches up with rustc::hir::def_id::INVALID_CRATE.
631635
// That constant is not available here unfortunately :(
@@ -651,6 +655,7 @@ impl fmt::Debug for FileMap {
651655
impl FileMap {
652656
pub fn new(name: FileName,
653657
name_was_remapped: bool,
658+
path: PathBuf,
654659
mut src: String,
655660
start_pos: BytePos) -> FileMap {
656661
remove_bom(&mut src);
@@ -664,6 +669,7 @@ impl FileMap {
664669
FileMap {
665670
name,
666671
name_was_remapped,
672+
path,
667673
crate_of_origin: 0,
668674
src: Some(Rc::new(src)),
669675
src_hash,
Lines changed: 16 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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)