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

expand_include: set .directory to dir of included file. #70184

Merged
merged 1 commit into from
Mar 21, 2020
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
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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This claim is a bit subtle, but this is what I inferred when reading the code in fully_expand_fragment. If this is not correct, then the alternative would be to add a flag to SyntaxExtension or something and deal with it in rustc_expand::expand.

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();
}