Skip to content

Commit

Permalink
Rollup merge of #70184 - Centril:include-mod-relativism, r=petrochenkov
Browse files Browse the repository at this point in the history
expand_include: set `.directory` to dir of included file.

Resolves the regression noted in #69838.

r? @petrochenkov
cc @eddyb @Mark-Simulacrum
  • Loading branch information
Centril authored Mar 21, 2020
2 parents a3771e4 + 0d018a5 commit 621f2b7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/librustc_builtin_macros/source_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use rustc_ast::token;
use rustc_ast::tokenstream::TokenStream;
use rustc_ast_pretty::pprust;
use rustc_expand::base::{self, *};
use rustc_expand::module::DirectoryOwnership;
use rustc_expand::panictry;
use rustc_parse::{self, new_sub_parser_from_file, parser::Parser};
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
use rustc_span::symbol::Symbol;
use rustc_span::{self, Pos, Span};

use smallvec::SmallVec;
use std::rc::Rc;

use rustc_data_structures::sync::Lrc;

Expand Down Expand Up @@ -101,7 +103,7 @@ pub fn expand_include<'cx>(
None => return DummyResult::any(sp),
};
// The file will be added to the code map by the parser
let file = match cx.resolve_path(file, sp) {
let mut file = match cx.resolve_path(file, sp) {
Ok(f) => f,
Err(mut err) => {
err.emit();
Expand All @@ -110,6 +112,15 @@ pub fn expand_include<'cx>(
};
let p = new_sub_parser_from_file(cx.parse_sess(), &file, None, sp);

// If in the included file we have e.g., `mod bar;`,
// then the path of `bar.rs` should be relative to the directory of `file`.
// See https://github.com/rust-lang/rust/pull/69838/files#r395217057 for a discussion.
// `MacroExpander::fully_expand_fragment` later restores, so "stack discipline" is maintained.
file.pop();
cx.current_expansion.directory_ownership = DirectoryOwnership::Owned { relative: None };
let mod_path = cx.current_expansion.module.mod_path.clone();
cx.current_expansion.module = Rc::new(ModuleData { mod_path, directory: file });

struct ExpandResult<'a> {
p: Parser<'a>,
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/macros/issue-69838-dir/bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// ignore-test -- this is an auxiliary file as part of another test.

pub fn i_am_in_bar() {}
3 changes: 3 additions & 0 deletions src/test/ui/macros/issue-69838-dir/included.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// ignore-test -- this is an auxiliary file as part of another test.

pub mod bar;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// check-pass

include!("issue-69838-dir/included.rs");

fn main() {
bar::i_am_in_bar();
}

0 comments on commit 621f2b7

Please sign in to comment.