Skip to content

Commit b35a995

Browse files
authored
Merge pull request #21179 from Veykril/push-xnrqmltklpxs
internal: Ensure proc-macro-api version check works with postcard across protocol versions
2 parents b657006 + 75d26b0 commit b35a995

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

src/tools/rust-analyzer/crates/hir-def/src/lang_item.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,12 @@ pub enum GenericRequirement {
161161

162162
macro_rules! language_item_table {
163163
(
164+
$LangItems:ident =>
164165
$( $(#[$attr:meta])* $lang_item:ident, $module:ident :: $name:ident, $method:ident, $target:ident, $generics:expr; )*
165166
) => {
166167
#[allow(non_snake_case)] // FIXME: Should we remove this?
167168
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
168-
pub struct LangItems {
169+
pub struct $LangItems {
169170
$(
170171
$(#[$attr])*
171172
pub $lang_item: Option<$target>,
@@ -219,7 +220,7 @@ macro_rules! language_item_table {
219220
}
220221
}
221222

222-
language_item_table! {
223+
language_item_table! { LangItems =>
223224
// Variant name, Name, Getter method name, Target Generic requirements;
224225
Sized, sym::sized, sized_trait, TraitId, GenericRequirement::Exact(0);
225226
MetaSized, sym::meta_sized, sized_trait, TraitId, GenericRequirement::Exact(0);

src/tools/rust-analyzer/crates/proc-macro-api/src/legacy_protocol/msg.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ use crate::{ProcMacroKind, codec::Codec};
1313
/// Represents requests sent from the client to the proc-macro-srv.
1414
#[derive(Debug, Serialize, Deserialize)]
1515
pub enum Request {
16+
// IMPORTANT: Keep his first, otherwise postcard will break as its not a self describing format
17+
// As such, this is the only request that needs to be supported across all protocol versions
18+
// and by keeping it first, we ensure it always has the same discriminant encoding in postcard
19+
/// Performs an API version check between the client and the server.
20+
/// Since [`VERSION_CHECK_VERSION`]
21+
ApiVersionCheck {},
22+
1623
/// Retrieves a list of macros from a given dynamic library.
1724
/// Since [`NO_VERSION_CHECK_VERSION`]
1825
ListMacros { dylib_path: Utf8PathBuf },
@@ -21,10 +28,6 @@ pub enum Request {
2128
/// Since [`NO_VERSION_CHECK_VERSION`]
2229
ExpandMacro(Box<ExpandMacro>),
2330

24-
/// Performs an API version check between the client and the server.
25-
/// Since [`VERSION_CHECK_VERSION`]
26-
ApiVersionCheck {},
27-
2831
/// Sets server-specific configurations.
2932
/// Since [`RUST_ANALYZER_SPAN_SUPPORT`]
3033
SetConfig(ServerConfig),
@@ -44,6 +47,13 @@ pub enum SpanMode {
4447
/// Represents responses sent from the proc-macro-srv to the client.
4548
#[derive(Debug, Serialize, Deserialize)]
4649
pub enum Response {
50+
// IMPORTANT: Keep his first, otherwise postcard will break as its not a self describing format
51+
// As such, this is the only request that needs to be supported across all protocol versions
52+
// and by keeping it first, we ensure it always has the same discriminant encoding in postcard
53+
/// Returns the API version supported by the server.
54+
/// Since [`NO_VERSION_CHECK_VERSION`]
55+
ApiVersionCheck(u32),
56+
4757
/// Returns a list of available macros in a dynamic library.
4858
/// Since [`NO_VERSION_CHECK_VERSION`]
4959
ListMacros(Result<Vec<(String, ProcMacroKind)>, String>),
@@ -52,10 +62,6 @@ pub enum Response {
5262
/// Since [`NO_VERSION_CHECK_VERSION`]
5363
ExpandMacro(Result<FlatTree, PanicMessage>),
5464

55-
/// Returns the API version supported by the server.
56-
/// Since [`NO_VERSION_CHECK_VERSION`]
57-
ApiVersionCheck(u32),
58-
5965
/// Confirms the application of a configuration update.
6066
/// Since [`RUST_ANALYZER_SPAN_SUPPORT`]
6167
SetConfig(ServerConfig),

src/tools/rust-analyzer/crates/proc-macro-srv/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,13 @@ impl ProcMacroSrvSpan for SpanId {
198198
type Server = server_impl::token_id::SpanIdServer;
199199

200200
fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server {
201-
Self::Server { call_site, def_site, mixed_site }
201+
Self::Server {
202+
call_site,
203+
def_site,
204+
mixed_site,
205+
tracked_env_vars: Default::default(),
206+
tracked_paths: Default::default(),
207+
}
202208
}
203209
}
204210
impl ProcMacroSrvSpan for Span {

src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! proc-macro server backend based on [`proc_macro_api::msg::SpanId`] as the backing span.
22
//! This backend is rather inflexible, used by RustRover and older rust-analyzer versions.
3-
use std::ops::{Bound, Range};
3+
use std::{
4+
collections::{HashMap, HashSet},
5+
ops::{Bound, Range},
6+
};
47

58
use intern::Symbol;
69
use proc_macro::bridge::server;
@@ -24,6 +27,10 @@ type Span = SpanId;
2427
pub struct FreeFunctions;
2528

2629
pub struct SpanIdServer {
30+
// FIXME: Report this back to the caller to track as dependencies
31+
pub tracked_env_vars: HashMap<Box<str>, Option<Box<str>>>,
32+
// FIXME: Report this back to the caller to track as dependencies
33+
pub tracked_paths: HashSet<Box<str>>,
2734
pub call_site: Span,
2835
pub def_site: Span,
2936
pub mixed_site: Span,
@@ -40,8 +47,13 @@ impl server::FreeFunctions for SpanIdServer {
4047
fn injected_env_var(&mut self, _: &str) -> Option<std::string::String> {
4148
None
4249
}
43-
fn track_env_var(&mut self, _var: &str, _value: Option<&str>) {}
44-
fn track_path(&mut self, _path: &str) {}
50+
fn track_env_var(&mut self, var: &str, value: Option<&str>) {
51+
self.tracked_env_vars.insert(var.into(), value.map(Into::into));
52+
}
53+
fn track_path(&mut self, path: &str) {
54+
self.tracked_paths.insert(path.into());
55+
}
56+
4557
fn literal_from_str(&mut self, s: &str) -> Result<Literal<Self::Span>, ()> {
4658
literal_from_str(s, self.call_site)
4759
}

0 commit comments

Comments
 (0)