fix some test due to resolve to where trait m impl

This commit is contained in:
bitgaoshu 2022-06-24 19:15:16 +08:00
parent 6ecabe352a
commit 9e6bff79f4
4 changed files with 41 additions and 35 deletions

View File

@ -281,7 +281,7 @@ use test_mod::*;
fn main() {
let test_struct = test_mod::TestStruct {};
TestTrait::test_method(&test_struct)
TestStruct::test_method(&test_struct)
}
"#,
);
@ -324,7 +324,7 @@ use test_mod::*;
fn main() {
let test_struct = test_mod::TestStruct {};
TestTrait::test_method(&test_struct, 12, 32u)
TestStruct::test_method(&test_struct, 12, 32u)
}
"#,
);
@ -367,7 +367,7 @@ use test_mod::*;
fn main() {
let test_struct = test_mod::TestStruct {};
TestTrait::test_method(test_struct, 12, 32u)
TestStruct::test_method(test_struct, 12, 32u)
}
"#,
);
@ -410,7 +410,7 @@ use test_mod::*;
fn main() {
let test_struct = test_mod::TestStruct {};
TestTrait::test_method(&mut test_struct, 12, 32u)
TestStruct::test_method(&mut test_struct, 12, 32u)
}
"#,
);
@ -480,7 +480,7 @@ use test_mod::*;
fn main() {
let test_struct = TestStruct {};
TestTrait::test_method::<()>(&test_struct)
TestStruct::test_method::<()>(&test_struct)
}
"#,
);

View File

@ -244,7 +244,7 @@ fn rename_mod(
fn rename_reference(
sema: &Semantics<RootDatabase>,
mut def: Definition,
def: Definition,
new_name: &str,
) -> Result<SourceChange> {
let ident_kind = IdentifierKind::classify(new_name)?;
@ -271,15 +271,43 @@ fn rename_reference(
}
}
let assoc_item = match def {
let def = convert_to_trait_def(def, sema);
let usages = def.usages(sema).all();
if !usages.is_empty() && ident_kind == IdentifierKind::Underscore {
cov_mark::hit!(rename_underscore_multiple);
bail!("Cannot rename reference to `_` as it is being referenced multiple times");
}
let mut source_change = SourceChange::default();
source_change.extend(usages.iter().map(|(&file_id, references)| {
(file_id, source_edit_from_references(references, def, new_name))
}));
let mut insert_def_edit = |def| {
let (file_id, edit) = source_edit_from_def(sema, def, new_name)?;
source_change.insert_source_edit(file_id, edit);
Ok(())
};
match def {
Definition::Local(l) => l
.associated_locals(sema.db)
.iter()
.try_for_each(|&local| insert_def_edit(Definition::Local(local))),
def => insert_def_edit(def),
}?;
Ok(source_change)
}
pub(crate) fn convert_to_trait_def(def: Definition, sema: &Semantics<RootDatabase>) -> Definition {
// HACK: resolve trait impl items to the item def of the trait definition
// so that we properly resolve all trait item references
let assoc_item = match def {
Definition::Function(it) => it.as_assoc_item(sema.db),
Definition::TypeAlias(it) => it.as_assoc_item(sema.db),
Definition::Const(it) => it.as_assoc_item(sema.db),
_ => None,
};
def = match assoc_item {
match assoc_item {
Some(assoc) => assoc
.containing_trait_impl(sema.db)
.and_then(|trait_| {
@ -305,31 +333,7 @@ fn rename_reference(
})
.unwrap_or(def),
None => def,
};
let usages = def.usages(sema).all();
if !usages.is_empty() && ident_kind == IdentifierKind::Underscore {
cov_mark::hit!(rename_underscore_multiple);
bail!("Cannot rename reference to `_` as it is being referenced multiple times");
}
let mut source_change = SourceChange::default();
source_change.extend(usages.iter().map(|(&file_id, references)| {
(file_id, source_edit_from_references(references, def, new_name))
}));
let mut insert_def_edit = |def| {
let (file_id, edit) = source_edit_from_def(sema, def, new_name)?;
source_change.insert_source_edit(file_id, edit);
Ok(())
};
match def {
Definition::Local(l) => l
.associated_locals(sema.db)
.iter()
.try_for_each(|&local| insert_def_edit(Definition::Local(local))),
def => insert_def_edit(def),
}?;
Ok(source_change)
}
pub fn source_edit_from_references(

View File

@ -619,7 +619,9 @@ impl<'a> FindUsages<'a> {
};
sink(file_id, reference)
}
Some(NameRefClass::Definition(def)) if def == self.def => {
Some(NameRefClass::Definition(def))
if crate::rename::convert_to_trait_def(def, self.sema) == self.def =>
{
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
let reference = FileReference {
range,

View File

@ -121,6 +121,6 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="keyword">let</span> <span class="struct">Packed</span> <span class="brace">{</span> <span class="field">a</span><span class="colon">:</span> <span class="keyword unsafe">ref</span> <span class="variable declaration reference">_a</span> <span class="brace">}</span> <span class="operator">=</span> <span class="variable">packed</span><span class="semicolon">;</span>
<span class="comment">// unsafe auto ref of packed field</span>
<span class="variable">packed</span><span class="operator">.</span><span class="field">a</span><span class="operator">.</span><span class="function associated reference trait unsafe">calls_autoref</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
<span class="variable">packed</span><span class="operator">.</span><span class="field">a</span><span class="operator">.</span><span class="function associated reference unsafe">calls_autoref</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
<span class="brace">}</span>
<span class="brace">}</span></code></pre>