mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 14:51:48 +00:00
Fix mod item in included file resolving incorrectly
This commit is contained in:
parent
d2a31acda1
commit
5edf7bddc6
6 changed files with 39 additions and 36 deletions
|
@ -66,7 +66,7 @@ impl ModDir {
|
||||||
attr_path: Option<&SmolStr>,
|
attr_path: Option<&SmolStr>,
|
||||||
) -> Result<(FileId, bool, ModDir), Box<[String]>> {
|
) -> Result<(FileId, bool, ModDir), Box<[String]>> {
|
||||||
let name = name.unescaped();
|
let name = name.unescaped();
|
||||||
let orig_file_id = file_id.original_file(db.upcast());
|
let orig_file_id = file_id.original_file_respecting_includes(db.upcast());
|
||||||
|
|
||||||
let mut candidate_files = ArrayVec::<_, 2>::new();
|
let mut candidate_files = ArrayVec::<_, 2>::new();
|
||||||
match attr_path {
|
match attr_path {
|
||||||
|
|
|
@ -578,18 +578,12 @@ fn include_expand(
|
||||||
tt: &tt::Subtree,
|
tt: &tt::Subtree,
|
||||||
span: SpanData,
|
span: SpanData,
|
||||||
) -> ExpandResult<tt::Subtree> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
let path = match parse_string(tt) {
|
let file_id = match include_input_to_file_id(db, arg_id, tt) {
|
||||||
Ok(it) => it,
|
Ok(it) => it,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e)
|
return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let file_id = match relative_file(db, arg_id, &path, false) {
|
|
||||||
Ok(file_id) => file_id,
|
|
||||||
Err(e) => {
|
|
||||||
return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
match parse_to_token_tree(
|
match parse_to_token_tree(
|
||||||
SpanAnchor { file_id, ast_id: ROOT_ERASED_FILE_AST_ID },
|
SpanAnchor { file_id, ast_id: ROOT_ERASED_FILE_AST_ID },
|
||||||
SyntaxContextId::ROOT,
|
SyntaxContextId::ROOT,
|
||||||
|
@ -603,19 +597,20 @@ fn include_expand(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn include_input_to_file_id(
|
||||||
|
db: &dyn ExpandDatabase,
|
||||||
|
arg_id: MacroCallId,
|
||||||
|
arg: &tt::Subtree,
|
||||||
|
) -> Result<FileId, ExpandError> {
|
||||||
|
relative_file(db, arg_id, &parse_string(arg)?, false)
|
||||||
|
}
|
||||||
|
|
||||||
fn include_bytes_expand(
|
fn include_bytes_expand(
|
||||||
_db: &dyn ExpandDatabase,
|
_db: &dyn ExpandDatabase,
|
||||||
_arg_id: MacroCallId,
|
_arg_id: MacroCallId,
|
||||||
tt: &tt::Subtree,
|
_tt: &tt::Subtree,
|
||||||
span: SpanData,
|
span: SpanData,
|
||||||
) -> ExpandResult<tt::Subtree> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
let _path = match parse_string(tt) {
|
|
||||||
Ok(it) => it,
|
|
||||||
Err(e) => {
|
|
||||||
return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// FIXME: actually read the file here if the user asked for macro expansion
|
// FIXME: actually read the file here if the user asked for macro expansion
|
||||||
let res = tt::Subtree {
|
let res = tt::Subtree {
|
||||||
delimiter: tt::Delimiter::dummy_invisible(),
|
delimiter: tt::Delimiter::dummy_invisible(),
|
||||||
|
|
|
@ -136,7 +136,7 @@ pub enum MacroDefKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
struct EagerCallInfo {
|
pub struct EagerCallInfo {
|
||||||
/// The expanded argument of the eager macro.
|
/// The expanded argument of the eager macro.
|
||||||
arg: Arc<tt::Subtree>,
|
arg: Arc<tt::Subtree>,
|
||||||
/// Call id of the eager macro's input file (this is the macro file for its fully expanded input).
|
/// Call id of the eager macro's input file (this is the macro file for its fully expanded input).
|
||||||
|
@ -176,6 +176,8 @@ pub trait HirFileIdExt {
|
||||||
/// expansion originated from.
|
/// expansion originated from.
|
||||||
fn original_file(self, db: &dyn db::ExpandDatabase) -> FileId;
|
fn original_file(self, db: &dyn db::ExpandDatabase) -> FileId;
|
||||||
|
|
||||||
|
fn original_file_respecting_includes(self, db: &dyn db::ExpandDatabase) -> FileId;
|
||||||
|
|
||||||
/// If this is a macro call, returns the syntax node of the call.
|
/// If this is a macro call, returns the syntax node of the call.
|
||||||
fn call_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<SyntaxNode>>;
|
fn call_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<SyntaxNode>>;
|
||||||
|
|
||||||
|
@ -215,6 +217,29 @@ impl HirFileIdExt for HirFileId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn original_file_respecting_includes(mut self, db: &dyn db::ExpandDatabase) -> FileId {
|
||||||
|
loop {
|
||||||
|
match self.repr() {
|
||||||
|
base_db::span::HirFileIdRepr::FileId(id) => break id,
|
||||||
|
base_db::span::HirFileIdRepr::MacroFile(file) => {
|
||||||
|
let loc = db.lookup_intern_macro_call(file.macro_call_id);
|
||||||
|
if loc.def.is_include() {
|
||||||
|
if let Some(eager) = &loc.eager {
|
||||||
|
if let Ok(it) = builtin_fn_macro::include_input_to_file_id(
|
||||||
|
db,
|
||||||
|
file.macro_call_id,
|
||||||
|
&eager.arg,
|
||||||
|
) {
|
||||||
|
break it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self = loc.kind.file_id();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn call_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<SyntaxNode>> {
|
fn call_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<SyntaxNode>> {
|
||||||
let macro_file = self.macro_file()?;
|
let macro_file = self.macro_file()?;
|
||||||
let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
|
let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
|
||||||
|
@ -473,7 +498,7 @@ impl MacroCallKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the file containing the macro invocation.
|
/// Returns the file containing the macro invocation.
|
||||||
fn file_id(&self) -> HirFileId {
|
pub fn file_id(&self) -> HirFileId {
|
||||||
match *self {
|
match *self {
|
||||||
MacroCallKind::FnLike { ast_id: InFile { file_id, .. }, .. }
|
MacroCallKind::FnLike { ast_id: InFile { file_id, .. }, .. }
|
||||||
| MacroCallKind::Derive { ast_id: InFile { file_id, .. }, .. }
|
| MacroCallKind::Derive { ast_id: InFile { file_id, .. }, .. }
|
||||||
|
|
|
@ -787,7 +787,6 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic] // FIXME
|
|
||||||
fn infer_builtin_macros_include_child_mod() {
|
fn infer_builtin_macros_include_child_mod() {
|
||||||
check_types(
|
check_types(
|
||||||
r#"
|
r#"
|
||||||
|
|
|
@ -67,22 +67,6 @@ macro_rules! m { () => {} } }
|
||||||
|
|
||||||
self::m!(); self::m2!();
|
self::m!(); self::m2!();
|
||||||
//^^ error: unresolved macro `self::m2!`
|
//^^ error: unresolved macro `self::m2!`
|
||||||
"#,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[should_panic] // FIXME: https://github.com/rust-lang/rust-analyzer/issues/14968
|
|
||||||
fn include_does_not_break_diagnostics() {
|
|
||||||
check_diagnostics(
|
|
||||||
r#"
|
|
||||||
//- minicore: include
|
|
||||||
//- /lib.rs crate:lib
|
|
||||||
include!("include-me.rs");
|
|
||||||
//- /include-me.rs
|
|
||||||
/// long doc that pushes the diagnostic range beyond the first file's text length
|
|
||||||
#[err]
|
|
||||||
mod prim_never {}
|
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ fn integrated_highlighting_benchmark() {
|
||||||
|
|
||||||
// Load rust-analyzer itself.
|
// Load rust-analyzer itself.
|
||||||
let workspace_to_load = project_root();
|
let workspace_to_load = project_root();
|
||||||
let file = "./crates/ide-db/src/apply_change.rs";
|
let file = "./crates/rust-analyzer/src/config.rs";
|
||||||
|
|
||||||
let cargo_config = CargoConfig::default();
|
let cargo_config = CargoConfig::default();
|
||||||
let load_cargo_config = LoadCargoConfig {
|
let load_cargo_config = LoadCargoConfig {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue