Merge pull request #19320 from alibektas/19254

Observe unsafeness when generating manual impls of former derives
This commit is contained in:
Chayim Refael Friedman 2025-03-17 22:05:07 +00:00 committed by GitHub
commit 31e412c290
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 48 additions and 3 deletions

View file

@ -5,7 +5,7 @@ use syntax::{
SyntaxKind::WHITESPACE, SyntaxKind::WHITESPACE,
T, T,
ast::{self, AstNode, HasName, make}, ast::{self, AstNode, HasName, make},
ted, ted::{self, Position},
}; };
use crate::{ use crate::{
@ -131,7 +131,7 @@ fn add_assist(
target, target,
|builder| { |builder| {
let insert_after = ted::Position::after(builder.make_mut(adt.clone()).syntax()); let insert_after = ted::Position::after(builder.make_mut(adt.clone()).syntax());
let impl_is_unsafe = trait_.map(|s| s.is_unsafe(ctx.db())).unwrap_or(false);
let impl_def_with_items = let impl_def_with_items =
impl_def_from_trait(&ctx.sema, adt, &annotated_name, trait_, replace_trait_path); impl_def_from_trait(&ctx.sema, adt, &annotated_name, trait_, replace_trait_path);
update_attribute(builder, old_derives, old_tree, old_trait_path, attr); update_attribute(builder, old_derives, old_tree, old_trait_path, attr);
@ -141,6 +141,12 @@ fn add_assist(
match (ctx.config.snippet_cap, impl_def_with_items) { match (ctx.config.snippet_cap, impl_def_with_items) {
(None, None) => { (None, None) => {
let impl_def = generate_trait_impl(adt, trait_path); let impl_def = generate_trait_impl(adt, trait_path);
if impl_is_unsafe {
ted::insert(
Position::first_child_of(impl_def.syntax()),
make::token(T![unsafe]),
);
}
ted::insert_all( ted::insert_all(
insert_after, insert_after,
@ -148,6 +154,12 @@ fn add_assist(
); );
} }
(None, Some((impl_def, _))) => { (None, Some((impl_def, _))) => {
if impl_is_unsafe {
ted::insert(
Position::first_child_of(impl_def.syntax()),
make::token(T![unsafe]),
);
}
ted::insert_all( ted::insert_all(
insert_after, insert_after,
vec![make::tokens::blank_line().into(), impl_def.syntax().clone().into()], vec![make::tokens::blank_line().into(), impl_def.syntax().clone().into()],
@ -156,6 +168,13 @@ fn add_assist(
(Some(cap), None) => { (Some(cap), None) => {
let impl_def = generate_trait_impl(adt, trait_path); let impl_def = generate_trait_impl(adt, trait_path);
if impl_is_unsafe {
ted::insert(
Position::first_child_of(impl_def.syntax()),
make::token(T![unsafe]),
);
}
if let Some(l_curly) = if let Some(l_curly) =
impl_def.assoc_item_list().and_then(|it| it.l_curly_token()) impl_def.assoc_item_list().and_then(|it| it.l_curly_token())
{ {
@ -169,6 +188,14 @@ fn add_assist(
} }
(Some(cap), Some((impl_def, first_assoc_item))) => { (Some(cap), Some((impl_def, first_assoc_item))) => {
let mut added_snippet = false; let mut added_snippet = false;
if impl_is_unsafe {
ted::insert(
Position::first_child_of(impl_def.syntax()),
make::token(T![unsafe]),
);
}
if let ast::AssocItem::Fn(ref func) = first_assoc_item { if let ast::AssocItem::Fn(ref func) = first_assoc_item {
if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast) if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast)
{ {
@ -1402,6 +1429,23 @@ impl core::fmt::Debug for Foo {
f.debug_struct("Foo").finish() f.debug_struct("Foo").finish()
} }
} }
"#,
)
}
#[test]
fn unsafeness_of_a_trait_observed() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: send, derive
#[derive(Sen$0d)]
pub struct Foo;
"#,
r#"
pub struct Foo;
unsafe impl Send for Foo {$0}
"#, "#,
) )
} }

View file

@ -212,6 +212,7 @@ pub fn add_trait_assoc_items_to_impl(
}); });
let assoc_item_list = impl_.get_or_create_assoc_item_list(); let assoc_item_list = impl_.get_or_create_assoc_item_list();
let mut first_item = None; let mut first_item = None;
for item in items { for item in items {
first_item.get_or_insert_with(|| item.clone()); first_item.get_or_insert_with(|| item.clone());

View file

@ -1276,7 +1276,7 @@ pub mod tokens {
pub(super) static SOURCE_FILE: LazyLock<Parse<SourceFile>> = LazyLock::new(|| { pub(super) static SOURCE_FILE: LazyLock<Parse<SourceFile>> = LazyLock::new(|| {
SourceFile::parse( SourceFile::parse(
"use crate::foo; const C: <()>::Item = ( true && true , true || true , 1 != 1, 2 == 2, 3 < 3, 4 <= 4, 5 > 5, 6 >= 6, !true, *p, &p , &mut p, async { let _ @ [] })\n;\n\nimpl A for B where: {}", "use crate::foo; const C: <()>::Item = ( true && true , true || true , 1 != 1, 2 == 2, 3 < 3, 4 <= 4, 5 > 5, 6 >= 6, !true, *p, &p , &mut p, async { let _ @ [] })\n;\n\nunsafe impl A for B where: {}",
Edition::CURRENT, Edition::CURRENT,
) )
}); });