mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Add test for invalidation of inferred types when typing inside function
This currently fails, but should work once we have hir::Expr.
This commit is contained in:
parent
3e42a15878
commit
a6f33b4ca5
2 changed files with 58 additions and 0 deletions
|
@ -87,6 +87,20 @@ fn module_from_source(
|
||||||
Ok(Some(Module::new(db, source_root_id, module_id)?))
|
Ok(Some(Module::new(db, source_root_id, module_id)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn function_from_position(
|
||||||
|
db: &impl HirDatabase,
|
||||||
|
position: FilePosition,
|
||||||
|
) -> Cancelable<Option<Function>> {
|
||||||
|
let file = db.source_file(position.file_id);
|
||||||
|
let fn_def = if let Some(f) = find_node_at_offset::<ast::FnDef>(file.syntax(), position.offset)
|
||||||
|
{
|
||||||
|
f
|
||||||
|
} else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
function_from_source(db, position.file_id, fn_def)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn function_from_source(
|
pub fn function_from_source(
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
use std::sync::Arc;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use std::path::{PathBuf, Path};
|
use std::path::{PathBuf, Path};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
|
use salsa::Database;
|
||||||
|
|
||||||
use ra_db::{SyntaxDatabase};
|
use ra_db::{SyntaxDatabase};
|
||||||
use ra_syntax::ast::{self, AstNode};
|
use ra_syntax::ast::{self, AstNode};
|
||||||
use test_utils::{project_dir, assert_eq_text, read_text};
|
use test_utils::{project_dir, assert_eq_text, read_text};
|
||||||
|
@ -217,3 +220,44 @@ fn ellipsize(mut text: String, max_len: usize) -> String {
|
||||||
fn test_data_dir() -> PathBuf {
|
fn test_data_dir() -> PathBuf {
|
||||||
project_dir().join("crates/ra_hir/src/ty/tests/data")
|
project_dir().join("crates/ra_hir/src/ty/tests/data")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic] // TODO this should work once hir::Expr is used
|
||||||
|
fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
|
||||||
|
let (mut db, pos) = MockDatabase::with_position(
|
||||||
|
"
|
||||||
|
//- /lib.rs
|
||||||
|
fn foo() -> i32 {
|
||||||
|
<|>1 + 1
|
||||||
|
}
|
||||||
|
",
|
||||||
|
);
|
||||||
|
let func = source_binder::function_from_position(&db, pos)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
{
|
||||||
|
let events = db.log_executed(|| {
|
||||||
|
func.infer(&db).unwrap();
|
||||||
|
});
|
||||||
|
assert!(format!("{:?}", events).contains("infer"))
|
||||||
|
}
|
||||||
|
|
||||||
|
let new_text = "
|
||||||
|
fn foo() -> i32 {
|
||||||
|
1
|
||||||
|
+
|
||||||
|
1
|
||||||
|
}
|
||||||
|
"
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
db.query_mut(ra_db::FileTextQuery)
|
||||||
|
.set(pos.file_id, Arc::new(new_text));
|
||||||
|
|
||||||
|
{
|
||||||
|
let events = db.log_executed(|| {
|
||||||
|
func.infer(&db).unwrap();
|
||||||
|
});
|
||||||
|
assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue