Skip to content

Commit 54911be

Browse files
authored
Rollup merge of rust-lang#70184 - Centril:include-mod-relativism, r=petrochenkov
expand_include: set `.directory` to dir of included file. Resolves the regression noted in rust-lang#69838. r? @petrochenkov cc @eddyb @Mark-Simulacrum
2 parents 632fa8e + 0d018a5 commit 54911be

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

src/librustc_builtin_macros/source_util.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ use rustc_ast::token;
44
use rustc_ast::tokenstream::TokenStream;
55
use rustc_ast_pretty::pprust;
66
use rustc_expand::base::{self, *};
7+
use rustc_expand::module::DirectoryOwnership;
78
use rustc_expand::panictry;
89
use rustc_parse::{self, new_sub_parser_from_file, parser::Parser};
910
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
1011
use rustc_span::symbol::Symbol;
1112
use rustc_span::{self, Pos, Span};
1213

1314
use smallvec::SmallVec;
15+
use std::rc::Rc;
1416

1517
use rustc_data_structures::sync::Lrc;
1618

@@ -101,7 +103,7 @@ pub fn expand_include<'cx>(
101103
None => return DummyResult::any(sp),
102104
};
103105
// The file will be added to the code map by the parser
104-
let file = match cx.resolve_path(file, sp) {
106+
let mut file = match cx.resolve_path(file, sp) {
105107
Ok(f) => f,
106108
Err(mut err) => {
107109
err.emit();
@@ -110,6 +112,15 @@ pub fn expand_include<'cx>(
110112
};
111113
let p = new_sub_parser_from_file(cx.parse_sess(), &file, None, sp);
112114

115+
// If in the included file we have e.g., `mod bar;`,
116+
// then the path of `bar.rs` should be relative to the directory of `file`.
117+
// See https://github.com/rust-lang/rust/pull/69838/files#r395217057 for a discussion.
118+
// `MacroExpander::fully_expand_fragment` later restores, so "stack discipline" is maintained.
119+
file.pop();
120+
cx.current_expansion.directory_ownership = DirectoryOwnership::Owned { relative: None };
121+
let mod_path = cx.current_expansion.module.mod_path.clone();
122+
cx.current_expansion.module = Rc::new(ModuleData { mod_path, directory: file });
123+
113124
struct ExpandResult<'a> {
114125
p: Parser<'a>,
115126
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// ignore-test -- this is an auxiliary file as part of another test.
2+
3+
pub fn i_am_in_bar() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// ignore-test -- this is an auxiliary file as part of another test.
2+
3+
pub mod bar;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// check-pass
2+
3+
include!("issue-69838-dir/included.rs");
4+
5+
fn main() {
6+
bar::i_am_in_bar();
7+
}

0 commit comments

Comments
 (0)