Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
34ddd59
Improve settings tree title and descriptions
joshka Jul 3, 2025
3540446
Fix gen panics doc template for debug_assert
A4-Tacks Jul 25, 2025
1d5a582
refactor: conpare text of name_ref instead of syntax name_ref
Hmikihiro Jul 27, 2025
b3ea82f
Migrate `inline_type_alias` assist to use `SyntaxEditor`
Hmikihiro Jul 27, 2025
b1b7077
remove ted from convert_tuple_struct_to_named_struct
Hmikihiro Jul 26, 2025
85486a7
migrate `fn edit_struct_def` in `convert_tuple_struct_to_named_struct…
Hmikihiro Jul 26, 2025
8f89872
Migrate `convert_tuple_struct_to_named_struct' assist to use `SyntaxE…
Hmikihiro Jul 27, 2025
b4b9f3e
Merge pull request #20300 from A4-Tacks/fix-debug_assert-doc-gen
ChayimFriedman2 Jul 29, 2025
8b214fb
fix: Do not require all rename definitions to be renameable
Veykril Jul 29, 2025
e45400a
Merge pull request #20333 from Veykril/push-xmulpqkxrytn
Veykril Jul 29, 2025
1532b37
Use GH app for authenticating sync PRs
Kobzol Jul 29, 2025
f7c3a89
Merge pull request #20335 from Kobzol/ci-gh-app
lnicola Jul 29, 2025
bad05ff
In generate_mut_trait_impl, don't add a tabstop if the client does no…
ChayimFriedman2 Jul 29, 2025
f5ecbb5
Merge pull request #20336 from ChayimFriedman2/mut-trait-impl-snippet
Veykril Jul 29, 2025
5043dc9
Merge pull request #20154 from joshka/jm/improve-setting-titles
Veykril Jul 29, 2025
ea1e24a
When displaying a projection into a type parameter that has bounds as…
ChayimFriedman2 Jul 29, 2025
b2d9f17
Merge pull request #20337 from ChayimFriedman2/double-inlay-hints
Veykril Jul 29, 2025
cca89bc
add `SyntaxFactory::record_expr` to hide clone_for_update
Hmikihiro Jul 29, 2025
08c6768
replace `make::` to `SyntaxFactory::` in `inline_type_alias`
Hmikihiro Jul 29, 2025
a9c4316
Merge pull request #20311 from Hmikihiro/migrate_convert_tuple_struct…
ShoyuVanilla Jul 30, 2025
cd0f643
Merge pull request #20314 from Hmikihiro/Migrate_inline_type_alias_to…
ShoyuVanilla Jul 30, 2025
2918a2b
Abtract away json protocol for proc-macro-srv
Veykril Jul 30, 2025
d71e972
add `SyntaxEditor::delete_all` to migrate utils.rs `add_trait_assoc_i…
Hmikihiro Jul 29, 2025
8971681
Properly clean proc-macro-srv proc-macro temp dir
Veykril Jul 31, 2025
1975c98
Reorganize proc-macro-srv
Veykril Jul 31, 2025
7543395
Add version command to proc-macro-srv
Veykril Jul 31, 2025
6bf9700
Merge pull request #20342 from Veykril/push-zysqtqskuxvr
Veykril Jul 31, 2025
a7b01aa
`cargo clippy --fix`
Veykril Jul 31, 2025
ddc1b9f
Merge pull request #20349 from Veykril/push-orvqsnqtttzv
Veykril Jul 31, 2025
7c60865
Merge pull request #20345 from Hmikihiro/Migrate_add_trait_assoc_item…
ShoyuVanilla Jul 31, 2025
053f681
Update documentation for overrideCommand config options
iorizu Aug 1, 2025
7d16726
Fix more docs
iorizu Aug 1, 2025
81c4086
Migrate `convert_from_to_tryfrom` assist to use `SyntaxEditor`
Hmikihiro Aug 2, 2025
e314bfa
Migrate `generate_delegate_methods` assist to use `SyntaxEditor`
Hmikihiro Aug 2, 2025
4c6e804
Merge pull request #20358 from iorizu/issue-20346
ChayimFriedman2 Aug 2, 2025
ac34f3d
When renaming a parameter to `self`, change callers to use method cal…
ChayimFriedman2 Jul 31, 2025
f360d6c
Merge pull request #20351 from ChayimFriedman2/rename-self1
Veykril Aug 2, 2025
279b464
Merge pull request #20364 from Hmikihiro/migrate_convert_from_to_tryfrom
Veykril Aug 3, 2025
e47f791
Merge pull request #20368 from Hmikihiro/migrate_delegate_methods
Veykril Aug 3, 2025
b373cb1
Migrate `generate_trait_from_impl` assist to use `SyntaxEditor`
Hmikihiro Aug 3, 2025
0d2a7e6
Merge pull request #20371 from Hmikihiro/migrate_generate_trait_from_…
Veykril Aug 3, 2025
277cf46
Remove unused functions from edit_in_place
Hmikihiro Aug 3, 2025
efbe625
Merge pull request #20372 from Hmikihiro/remove_unused_fn_from_edit_i…
Veykril Aug 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reorganize proc-macro-srv
  • Loading branch information
Veykril committed Jul 31, 2025
commit 1975c98b73fd8ed79792557605f76ce053710dd8
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ impl SpanTransformer for SpanTrans {
_: &mut Self::Table,
span: Self::Span,
) -> proc_macro_api::legacy_protocol::SpanId {
proc_macro_api::legacy_protocol::SpanId(span.0 as u32)
proc_macro_api::legacy_protocol::SpanId(span.0)
}
fn span_for_token_id(
_: &Self::Table,
id: proc_macro_api::legacy_protocol::SpanId,
) -> Self::Span {
SpanId(id.0 as u32)
SpanId(id.0)
}
}

Expand Down Expand Up @@ -99,7 +99,7 @@ fn run_json() -> io::Result<()> {
lib,
&env,
current_dir,
macro_name,
&macro_name,
macro_body,
attributes,
def_site,
Expand All @@ -112,6 +112,7 @@ fn run_json() -> io::Result<()> {
CURRENT_API_VERSION,
)
})
.map_err(|e| e.into_string().unwrap_or_default())
.map_err(msg::PanicMessage)
}),
SpanMode::RustAnalyzer => msg::Response::ExpandMacroExtended({
Expand All @@ -130,7 +131,7 @@ fn run_json() -> io::Result<()> {
lib,
&env,
current_dir,
macro_name,
&macro_name,
macro_body,
attributes,
def_site,
Expand All @@ -151,6 +152,7 @@ fn run_json() -> io::Result<()> {
tree,
span_data_table,
})
.map_err(|e| e.into_string().unwrap_or_default())
.map_err(msg::PanicMessage)
}),
}
Expand Down
203 changes: 97 additions & 106 deletions src/tools/rust-analyzer/crates/proc-macro-srv/src/dylib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Handles dynamic library loading for proc macro

mod proc_macros;
mod version;

use proc_macro::bridge;
Expand All @@ -10,57 +11,56 @@ use libloading::Library;
use object::Object;
use paths::{Utf8Path, Utf8PathBuf};

use crate::{ProcMacroKind, ProcMacroSrvSpan, proc_macros::ProcMacros, server_impl::TopSubtree};
use crate::{
PanicMessage, ProcMacroKind, ProcMacroSrvSpan, dylib::proc_macros::ProcMacros,
server_impl::TopSubtree,
};

/// Loads dynamic library in platform dependent manner.
///
/// For unix, you have to use RTLD_DEEPBIND flag to escape problems described
/// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample)
/// and [here](https://github.com/rust-lang/rust/issues/60593).
///
/// Usage of RTLD_DEEPBIND
/// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample/issues/1)
///
/// It seems that on Windows that behaviour is default, so we do nothing in that case.
///
/// # Safety
///
/// The caller is responsible for ensuring that the path is valid proc-macro library
#[cfg(windows)]
unsafe fn load_library(file: &Utf8Path) -> Result<Library, libloading::Error> {
// SAFETY: The caller is responsible for ensuring that the path is valid proc-macro library
unsafe { Library::new(file) }
pub(crate) struct Expander {
inner: ProcMacroLibrary,
modified_time: SystemTime,
}

/// Loads dynamic library in platform dependent manner.
///
/// For unix, you have to use RTLD_DEEPBIND flag to escape problems described
/// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample)
/// and [here](https://github.com/rust-lang/rust/issues/60593).
///
/// Usage of RTLD_DEEPBIND
/// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample/issues/1)
///
/// It seems that on Windows that behaviour is default, so we do nothing in that case.
///
/// # Safety
///
/// The caller is responsible for ensuring that the path is valid proc-macro library
#[cfg(unix)]
unsafe fn load_library(file: &Utf8Path) -> Result<Library, libloading::Error> {
// not defined by POSIX, different values on mips vs other targets
#[cfg(target_env = "gnu")]
use libc::RTLD_DEEPBIND;
use libloading::os::unix::Library as UnixLibrary;
// defined by POSIX
use libloading::os::unix::RTLD_NOW;
impl Expander {
pub(crate) fn new(
temp_dir: &TempDir,
lib: &Utf8Path,
) -> Result<Expander, LoadProcMacroDylibError> {
// Some libraries for dynamic loading require canonicalized path even when it is
// already absolute
let lib = lib.canonicalize_utf8()?;
let modified_time = fs::metadata(&lib).and_then(|it| it.modified())?;

// MUSL and bionic don't have it..
#[cfg(not(target_env = "gnu"))]
const RTLD_DEEPBIND: std::os::raw::c_int = 0x0;
let path = ensure_file_with_lock_free_access(temp_dir, &lib)?;
let library = ProcMacroLibrary::open(path.as_ref())?;

// SAFETY: The caller is responsible for ensuring that the path is valid proc-macro library
unsafe { UnixLibrary::open(Some(file), RTLD_NOW | RTLD_DEEPBIND).map(|lib| lib.into()) }
Ok(Expander { inner: library, modified_time })
}

pub(crate) fn expand<S: ProcMacroSrvSpan>(
&self,
macro_name: &str,
macro_body: TopSubtree<S>,
attributes: Option<TopSubtree<S>>,
def_site: S,
call_site: S,
mixed_site: S,
) -> Result<TopSubtree<S>, PanicMessage>
where
<S::Server as bridge::server::Types>::TokenStream: Default,
{
self.inner
.proc_macros
.expand(macro_name, macro_body, attributes, def_site, call_site, mixed_site)
}

pub(crate) fn list_macros(&self) -> impl Iterator<Item = (&str, ProcMacroKind)> {
self.inner.proc_macros.list_macros()
}

pub(crate) fn modified_time(&self) -> SystemTime {
self.modified_time
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -134,57 +134,6 @@ impl ProcMacroLibrary {
}
}

// Drop order matters as we can't remove the dylib before the library is unloaded
pub(crate) struct Expander {
inner: ProcMacroLibrary,
_remove_on_drop: RemoveFileOnDrop,
modified_time: SystemTime,
}

impl Expander {
pub(crate) fn new(
temp_dir: &TempDir,
lib: &Utf8Path,
) -> Result<Expander, LoadProcMacroDylibError> {
// Some libraries for dynamic loading require canonicalized path even when it is
// already absolute
let lib = lib.canonicalize_utf8()?;
let modified_time = fs::metadata(&lib).and_then(|it| it.modified())?;

let path = ensure_file_with_lock_free_access(temp_dir, &lib)?;
let library = ProcMacroLibrary::open(path.as_ref())?;

Ok(Expander { inner: library, _remove_on_drop: RemoveFileOnDrop(path), modified_time })
}

pub(crate) fn expand<S: ProcMacroSrvSpan>(
&self,
macro_name: &str,
macro_body: TopSubtree<S>,
attributes: Option<TopSubtree<S>>,
def_site: S,
call_site: S,
mixed_site: S,
) -> Result<TopSubtree<S>, String>
where
<S::Server as bridge::server::Types>::TokenStream: Default,
{
let result = self
.inner
.proc_macros
.expand(macro_name, macro_body, attributes, def_site, call_site, mixed_site);
result.map_err(|e| e.into_string().unwrap_or_default())
}

pub(crate) fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
self.inner.proc_macros.list_macros()
}

pub(crate) fn modified_time(&self) -> SystemTime {
self.modified_time
}
}

fn invalid_data_err(e: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, e)
}
Expand Down Expand Up @@ -214,15 +163,6 @@ fn find_registrar_symbol(obj: &object::File<'_>) -> object::Result<Option<String
}))
}

struct RemoveFileOnDrop(Utf8PathBuf);
impl Drop for RemoveFileOnDrop {
fn drop(&mut self) {
#[cfg(windows)]
std::fs::remove_file(&self.0).unwrap();
_ = self.0;
}
}

/// Copy the dylib to temp directory to prevent locking in Windows
#[cfg(windows)]
fn ensure_file_with_lock_free_access(
Expand Down Expand Up @@ -259,3 +199,54 @@ fn ensure_file_with_lock_free_access(
) -> io::Result<Utf8PathBuf> {
Ok(path.to_owned())
}

/// Loads dynamic library in platform dependent manner.
///
/// For unix, you have to use RTLD_DEEPBIND flag to escape problems described
/// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample)
/// and [here](https://github.com/rust-lang/rust/issues/60593).
///
/// Usage of RTLD_DEEPBIND
/// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample/issues/1)
///
/// It seems that on Windows that behaviour is default, so we do nothing in that case.
///
/// # Safety
///
/// The caller is responsible for ensuring that the path is valid proc-macro library
#[cfg(windows)]
unsafe fn load_library(file: &Utf8Path) -> Result<Library, libloading::Error> {
// SAFETY: The caller is responsible for ensuring that the path is valid proc-macro library
unsafe { Library::new(file) }
}

/// Loads dynamic library in platform dependent manner.
///
/// For unix, you have to use RTLD_DEEPBIND flag to escape problems described
/// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample)
/// and [here](https://github.com/rust-lang/rust/issues/60593).
///
/// Usage of RTLD_DEEPBIND
/// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample/issues/1)
///
/// It seems that on Windows that behaviour is default, so we do nothing in that case.
///
/// # Safety
///
/// The caller is responsible for ensuring that the path is valid proc-macro library
#[cfg(unix)]
unsafe fn load_library(file: &Utf8Path) -> Result<Library, libloading::Error> {
// not defined by POSIX, different values on mips vs other targets
#[cfg(target_env = "gnu")]
use libc::RTLD_DEEPBIND;
use libloading::os::unix::Library as UnixLibrary;
// defined by POSIX
use libloading::os::unix::RTLD_NOW;

// MUSL and bionic don't have it..
#[cfg(not(target_env = "gnu"))]
const RTLD_DEEPBIND: std::os::raw::c_int = 0x0;

// SAFETY: The caller is responsible for ensuring that the path is valid proc-macro library
unsafe { UnixLibrary::open(Some(file), RTLD_NOW | RTLD_DEEPBIND).map(|lib| lib.into()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,13 @@ impl ProcMacros {
Err(bridge::PanicMessage::String(format!("proc-macro `{macro_name}` is missing")).into())
}

pub(crate) fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
self.0
.iter()
.map(|proc_macro| match proc_macro {
bridge::client::ProcMacro::CustomDerive { trait_name, .. } => {
(trait_name.to_string(), ProcMacroKind::CustomDerive)
}
bridge::client::ProcMacro::Bang { name, .. } => {
(name.to_string(), ProcMacroKind::Bang)
}
bridge::client::ProcMacro::Attr { name, .. } => {
(name.to_string(), ProcMacroKind::Attr)
}
})
.collect()
pub(crate) fn list_macros(&self) -> impl Iterator<Item = (&str, ProcMacroKind)> {
self.0.iter().map(|proc_macro| match *proc_macro {
bridge::client::ProcMacro::CustomDerive { trait_name, .. } => {
(trait_name, ProcMacroKind::CustomDerive)
}
bridge::client::ProcMacro::Bang { name, .. } => (name, ProcMacroKind::Bang),
bridge::client::ProcMacro::Attr { name, .. } => (name, ProcMacroKind::Attr),
})
}
}
Loading