3509: Prevent include! macro include itself r=matklad a=edwin0cheng

This PR prevent `include` macro including itself. 

Note: It **does not** prevent a cyclic include: 
```rust
// foo.rs
include!("bar.rs")

// bar.rs
include!("foo.rs")
```

Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
This commit is contained in:
bors[bot] 2020-03-07 11:09:54 +00:00 committed by GitHub
commit 013e908056
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View file

@ -279,7 +279,12 @@ fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Opti
let call_site = call_id.as_file().original_file(db);
let path = RelativePath::new(&path);
db.resolve_relative_path(call_site, &path)
let res = db.resolve_relative_path(call_site, &path)?;
// Prevent include itself
if res == call_site {
return None;
}
Some(res)
}
fn include_expand(

View file

@ -510,6 +510,24 @@ fn bar() -> u32 {0}
assert_eq!("{unknown}", type_at_pos(&db, pos));
}
#[test]
fn infer_builtin_macros_include_itself_should_failed() {
let (db, pos) = TestDB::with_position(
r#"
//- /main.rs
#[rustc_builtin_macro]
macro_rules! include {() => {}}
include!("main.rs");
fn main() {
0<|>
}
"#,
);
assert_eq!("i32", type_at_pos(&db, pos));
}
#[test]
fn infer_builtin_macros_concat_with_lazy() {
assert_snapshot!(