From 5f80364ede59d2507205246b383c25cb4dfb67ff Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 17 Mar 2021 21:56:09 +0100 Subject: Improve diagnostic when including nonexistent file --- crates/hir_def/src/body/tests.rs | 2 +- crates/hir_def/src/nameres/tests/diagnostics.rs | 2 +- crates/hir_expand/src/builtin_macro.rs | 21 +++++++++++---------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index f8e6f70e8..faa133297 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs @@ -137,7 +137,7 @@ fn f() { include!(invalid); //^^^^^^^^^^^^^^^^^ could not convert tokens include!("does not exist"); - //^^^^^^^^^^^^^^^^^^^^^^^^^^ could not convert tokens + //^^^^^^^^^^^^^^^^^^^^^^^^^^ failed to load file `does not exist` env!(invalid); //^^^^^^^^^^^^^ could not convert tokens diff --git a/crates/hir_def/src/nameres/tests/diagnostics.rs b/crates/hir_def/src/nameres/tests/diagnostics.rs index 6becd9ff1..a89061c2e 100644 --- a/crates/hir_def/src/nameres/tests/diagnostics.rs +++ b/crates/hir_def/src/nameres/tests/diagnostics.rs @@ -196,7 +196,7 @@ fn builtin_macro_fails_expansion() { macro_rules! include { () => {} } include!("doesntexist"); - //^^^^^^^^^^^^^^^^^^^^^^^^ could not convert tokens + //^^^^^^^^^^^^^^^^^^^^^^^^ failed to load file `doesntexist` "#, ); } 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( fn relative_file( db: &dyn AstDatabase, call_id: MacroCallId, - path: &str, + path_str: &str, allow_recursion: bool, -) -> Option { +) -> Result { let call_site = call_id.as_file().original_file(db); - let path = AnchoredPath { anchor: call_site, path }; - let res = db.resolve_path(path)?; + let path = AnchoredPath { anchor: call_site, path: path_str }; + let res = db + .resolve_path(path) + .ok_or_else(|| mbe::ExpandError::Other(format!("failed to load file `{}`", path_str)))?; // Prevent include itself if res == call_site && !allow_recursion { - None + Err(mbe::ExpandError::Other(format!("recursive inclusion of `{}`", path_str))) } else { - Some(res) + Ok(res) } } @@ -364,8 +366,7 @@ fn include_expand( ) -> ExpandResult> { let res = (|| { let path = parse_string(tt)?; - let file_id = relative_file(db, arg_id.into(), &path, false) - .ok_or_else(|| mbe::ExpandError::ConversionError)?; + let file_id = relative_file(db, arg_id.into(), &path, false)?; Ok(parse_to_token_tree(&db.file_text(file_id)) .ok_or_else(|| mbe::ExpandError::ConversionError)? @@ -417,8 +418,8 @@ fn include_str_expand( // Ideally, we'd be able to offer a precise expansion if the user asks for macro // expansion. let file_id = match relative_file(db, arg_id.into(), &path, true) { - Some(file_id) => file_id, - None => { + Ok(file_id) => file_id, + Err(_) => { return ExpandResult::ok(Some((quote!(""), FragmentKind::Expr))); } }; -- cgit v1.2.3