aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_expand
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-03-17 20:56:09 +0000
committerJonas Schievink <[email protected]>2021-03-17 20:56:09 +0000
commit5f80364ede59d2507205246b383c25cb4dfb67ff (patch)
tree6b42a73a8ba0a274200c1e8d5d0567fcc4116146 /crates/hir_expand
parent9d691530d556bdc40262585383a1b18d3a1de07e (diff)
Improve diagnostic when including nonexistent file
Diffstat (limited to 'crates/hir_expand')
-rw-r--r--crates/hir_expand/src/builtin_macro.rs21
1 files changed, 11 insertions, 10 deletions
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs
index 2a79c892b..fce09a9e7 100644
--- a/crates/hir_expand/src/builtin_macro.rs
+++ b/crates/hir_expand/src/builtin_macro.rs
@@ -333,17 +333,19 @@ fn concat_expand(
333fn relative_file( 333fn relative_file(
334 db: &dyn AstDatabase, 334 db: &dyn AstDatabase,
335 call_id: MacroCallId, 335 call_id: MacroCallId,
336 path: &str, 336 path_str: &str,
337 allow_recursion: bool, 337 allow_recursion: bool,
338) -> Option<FileId> { 338) -> Result<FileId, mbe::ExpandError> {
339 let call_site = call_id.as_file().original_file(db); 339 let call_site = call_id.as_file().original_file(db);
340 let path = AnchoredPath { anchor: call_site, path }; 340 let path = AnchoredPath { anchor: call_site, path: path_str };
341 let res = db.resolve_path(path)?; 341 let res = db
342 .resolve_path(path)
343 .ok_or_else(|| mbe::ExpandError::Other(format!("failed to load file `{}`", path_str)))?;
342 // Prevent include itself 344 // Prevent include itself
343 if res == call_site && !allow_recursion { 345 if res == call_site && !allow_recursion {
344 None 346 Err(mbe::ExpandError::Other(format!("recursive inclusion of `{}`", path_str)))
345 } else { 347 } else {
346 Some(res) 348 Ok(res)
347 } 349 }
348} 350}
349 351
@@ -364,8 +366,7 @@ fn include_expand(
364) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> { 366) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> {
365 let res = (|| { 367 let res = (|| {
366 let path = parse_string(tt)?; 368 let path = parse_string(tt)?;
367 let file_id = relative_file(db, arg_id.into(), &path, false) 369 let file_id = relative_file(db, arg_id.into(), &path, false)?;
368 .ok_or_else(|| mbe::ExpandError::ConversionError)?;
369 370
370 Ok(parse_to_token_tree(&db.file_text(file_id)) 371 Ok(parse_to_token_tree(&db.file_text(file_id))
371 .ok_or_else(|| mbe::ExpandError::ConversionError)? 372 .ok_or_else(|| mbe::ExpandError::ConversionError)?
@@ -417,8 +418,8 @@ fn include_str_expand(
417 // Ideally, we'd be able to offer a precise expansion if the user asks for macro 418 // Ideally, we'd be able to offer a precise expansion if the user asks for macro
418 // expansion. 419 // expansion.
419 let file_id = match relative_file(db, arg_id.into(), &path, true) { 420 let file_id = match relative_file(db, arg_id.into(), &path, true) {
420 Some(file_id) => file_id, 421 Ok(file_id) => file_id,
421 None => { 422 Err(_) => {
422 return ExpandResult::ok(Some((quote!(""), FragmentKind::Expr))); 423 return ExpandResult::ok(Some((quote!(""), FragmentKind::Expr)));
423 } 424 }
424 }; 425 };