Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
//! Defines messages for cross-process message passing based on `ndjson` wire protocol
pub(crate) mod flat;
pub use self::flat::*;

use std::io::{self, BufRead, Write};

use paths::Utf8PathBuf;
use serde::de::DeserializeOwned;
use serde_derive::{Deserialize, Serialize};

use crate::{ProcMacroKind, transport::json};

/// Represents requests sent from the client to the proc-macro-srv.
#[derive(Debug, Serialize, Deserialize)]
pub enum Request {
    // IMPORTANT: Keep his first, otherwise postcard will break as its not a self describing format
    // As such, this is the only request that needs to be supported across all protocol versions
    // and by keeping it first, we ensure it always has the same discriminant encoding in postcard
    /// Performs an API version check between the client and the server.
    /// Since [`crate::version::VERSION_CHECK_VERSION`]
    ApiVersionCheck {},

    /// Retrieves a list of macros from a given dynamic library.
    /// Since [`crate::version::NO_VERSION_CHECK_VERSION`]
    ListMacros { dylib_path: Utf8PathBuf },

    /// Expands a procedural macro.
    /// Since [`crate::version::NO_VERSION_CHECK_VERSION`]
    ExpandMacro(Box<ExpandMacro>),

    /// Sets server-specific configurations.
    /// Since [`crate::version::RUST_ANALYZER_SPAN_SUPPORT`]
    SetConfig(ServerConfig),
}

/// Defines the mode used for handling span data.
#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum SpanMode {
    /// Default mode, where spans are identified by an ID.
    #[default]
    Id,

    /// Rust Analyzer-specific span handling mode.
    RustAnalyzer,
}

/// Represents responses sent from the proc-macro-srv to the client.
#[derive(Debug, Serialize, Deserialize)]
pub enum Response {
    // IMPORTANT: Keep his first, otherwise postcard will break as its not a self describing format
    // As such, this is the only request that needs to be supported across all protocol versions
    // and by keeping it first, we ensure it always has the same discriminant encoding in postcard
    /// Returns the API version supported by the server.
    /// Since [`crate::version::NO_VERSION_CHECK_VERSION`]
    ApiVersionCheck(u32),

    /// Returns a list of available macros in a dynamic library.
    /// Since [`crate::version::NO_VERSION_CHECK_VERSION`]
    ListMacros(Result<Vec<(String, ProcMacroKind)>, String>),

    /// Returns result of a macro expansion.
    /// Since [`crate::version::NO_VERSION_CHECK_VERSION`]
    ExpandMacro(Result<FlatTree, PanicMessage>),

    /// Confirms the application of a configuration update.
    /// Since [`crate::version::RUST_ANALYZER_SPAN_SUPPORT`]
    SetConfig(ServerConfig),

    /// Returns the result of a macro expansion, including extended span data.
    /// Since [`crate::version::RUST_ANALYZER_SPAN_SUPPORT`]
    ExpandMacroExtended(Result<ExpandMacroExtended, PanicMessage>),
}

/// Configuration settings for the proc-macro-srv.
#[derive(Debug, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct ServerConfig {
    /// Defines how span data should be handled.
    pub span_mode: SpanMode,
}

/// Represents an extended macro expansion response, including span data mappings.
#[derive(Debug, Serialize, Deserialize)]
pub struct ExpandMacroExtended {
    /// The expanded syntax tree.
    pub tree: FlatTree,
    /// Additional span data mappings.
    pub span_data_table: Vec<u32>,
}

/// Represents an error message when a macro expansion results in a panic.
#[derive(Debug, Serialize, Deserialize)]
pub struct PanicMessage(pub String);

/// Represents a macro expansion request sent from the client.
#[derive(Debug, Serialize, Deserialize)]
pub struct ExpandMacro {
    /// The path to the dynamic library containing the macro.
    pub lib: Utf8PathBuf,
    /// Environment variables to set during macro expansion.
    pub env: Vec<(String, String)>,
    /// The current working directory for the macro expansion.
    pub current_dir: Option<String>,
    /// Macro expansion data, including the macro body, name and attributes.
    #[serde(flatten)]
    pub data: ExpandMacroData,
}

/// Represents the input data required for expanding a macro.
#[derive(Debug, Serialize, Deserialize)]
pub struct ExpandMacroData {
    /// Argument of macro call.
    ///
    /// In custom derive this will be a struct or enum; in attribute-like macro - underlying
    /// item; in function-like macro - the macro body.
    pub macro_body: FlatTree,

    /// Name of macro to expand.
    ///
    /// In custom derive this is the name of the derived trait (`Serialize`, `Getters`, etc.).
    /// In attribute-like and function-like macros - single name of macro itself (`show_streams`).
    pub macro_name: String,

    /// Possible attributes for the attribute-like macros.
    pub attributes: Option<FlatTree>,
    /// marker for serde skip stuff
    #[serde(skip_serializing_if = "ExpnGlobals::skip_serializing_if")]
    #[serde(default)]
    pub has_global_spans: ExpnGlobals,
    /// Table of additional span data.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    #[serde(default)]
    pub span_data_table: Vec<u32>,
}

/// Represents global expansion settings, including span resolution.
#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize)]
pub struct ExpnGlobals {
    /// Determines whether to serialize the expansion settings.
    #[serde(skip_serializing)]
    #[serde(default)]
    pub serialize: bool,
    /// Defines the `def_site` span location.
    pub def_site: usize,
    /// Defines the `call_site` span location.
    pub call_site: usize,
    /// Defines the `mixed_site` span location.
    pub mixed_site: usize,
}

impl ExpnGlobals {
    fn skip_serializing_if(&self) -> bool {
        !self.serialize
    }
}

pub trait Message: serde::Serialize + DeserializeOwned {
    type Buf;
    fn read(inp: &mut dyn BufRead, buf: &mut Self::Buf) -> io::Result<Option<Self>>;
    fn write(self, out: &mut dyn Write) -> io::Result<()>;
}

impl Message for Request {
    type Buf = String;

    fn read(inp: &mut dyn BufRead, buf: &mut Self::Buf) -> io::Result<Option<Self>> {
        Ok(match json::read(inp, buf)? {
            None => None,
            Some(buf) => Some(json::decode(buf)?),
        })
    }
    fn write(self, out: &mut dyn Write) -> io::Result<()> {
        let value = json::encode(&self)?;
        json::write(out, &value)
    }
}

impl Message for Response {
    type Buf = String;

    fn read(inp: &mut dyn BufRead, buf: &mut Self::Buf) -> io::Result<Option<Self>> {
        Ok(match json::read(inp, buf)? {
            None => None,
            Some(buf) => Some(json::decode(buf)?),
        })
    }
    fn write(self, out: &mut dyn Write) -> io::Result<()> {
        let value = json::encode(&self)?;
        json::write(out, &value)
    }
}

#[cfg(test)]
mod tests {
    use intern::Symbol;
    use span::{ROOT_ERASED_FILE_AST_ID, Span, SpanAnchor, SyntaxContext, TextRange, TextSize};
    use tt::{
        Delimiter, DelimiterKind, Ident, Leaf, Literal, Punct, Spacing, TopSubtree,
        TopSubtreeBuilder,
    };

    use crate::version;

    use super::*;

    fn make_ctx() -> SyntaxContext {
        // SAFETY: Tests do not use a Database, so this won't ever be used within salsa.
        unsafe { SyntaxContext::from_u32(0) }
    }

    fn fixture_token_tree_top_many_none() -> TopSubtree {
        let anchor = SpanAnchor {
            file_id: span::EditionedFileId::new(
                span::FileId::from_raw(0xe4e4e),
                span::Edition::CURRENT,
            ),
            ast_id: ROOT_ERASED_FILE_AST_ID,
        };

        let mut builder = TopSubtreeBuilder::new(Delimiter {
            open: Span { range: TextRange::empty(TextSize::new(0)), anchor, ctx: make_ctx() },
            close: Span { range: TextRange::empty(TextSize::new(0)), anchor, ctx: make_ctx() },
            kind: DelimiterKind::Invisible,
        });

        builder.push(
            Ident {
                sym: Symbol::intern("struct"),
                span: Span {
                    range: TextRange::at(TextSize::new(0), TextSize::of("struct")),
                    anchor,
                    ctx: make_ctx(),
                },
                is_raw: tt::IdentIsRaw::No,
            }
            .into(),
        );
        builder.push(
            Ident {
                sym: Symbol::intern("Foo"),
                span: Span {
                    range: TextRange::at(TextSize::new(5), TextSize::of("r#Foo")),
                    anchor,
                    ctx: make_ctx(),
                },
                is_raw: tt::IdentIsRaw::Yes,
            }
            .into(),
        );
        builder.push(Leaf::Literal(Literal::new_no_suffix(
            "Foo",
            Span {
                range: TextRange::at(TextSize::new(10), TextSize::of("\"Foo\"")),
                anchor,
                ctx: make_ctx(),
            },
            tt::LitKind::Str,
        )));
        builder.push(Leaf::Punct(Punct {
            char: '@',
            span: Span {
                range: TextRange::at(TextSize::new(13), TextSize::of('@')),
                anchor,
                ctx: make_ctx(),
            },
            spacing: Spacing::Joint,
        }));
        builder.open(
            DelimiterKind::Brace,
            Span {
                range: TextRange::at(TextSize::new(14), TextSize::of('{')),
                anchor,
                ctx: make_ctx(),
            },
        );
        builder.open(
            DelimiterKind::Bracket,
            Span {
                range: TextRange::at(TextSize::new(15), TextSize::of('[')),
                anchor,
                ctx: make_ctx(),
            },
        );
        builder.push(Leaf::Literal(Literal::new(
            "0",
            Span {
                range: TextRange::at(TextSize::new(16), TextSize::of("0u32")),
                anchor,
                ctx: make_ctx(),
            },
            tt::LitKind::Integer,
            "u32",
        )));
        builder.close(Span {
            range: TextRange::at(TextSize::new(20), TextSize::of(']')),
            anchor,
            ctx: make_ctx(),
        });

        builder.close(Span {
            range: TextRange::at(TextSize::new(21), TextSize::of('}')),
            anchor,
            ctx: make_ctx(),
        });

        builder.build()
    }

    fn fixture_token_tree_top_empty_none() -> TopSubtree {
        let anchor = SpanAnchor {
            file_id: span::EditionedFileId::new(
                span::FileId::from_raw(0xe4e4e),
                span::Edition::CURRENT,
            ),
            ast_id: ROOT_ERASED_FILE_AST_ID,
        };

        let builder = TopSubtreeBuilder::new(Delimiter {
            open: Span { range: TextRange::empty(TextSize::new(0)), anchor, ctx: make_ctx() },
            close: Span { range: TextRange::empty(TextSize::new(0)), anchor, ctx: make_ctx() },
            kind: DelimiterKind::Invisible,
        });

        builder.build()
    }

    fn fixture_token_tree_top_empty_brace() -> TopSubtree {
        let anchor = SpanAnchor {
            file_id: span::EditionedFileId::new(
                span::FileId::from_raw(0xe4e4e),
                span::Edition::CURRENT,
            ),
            ast_id: ROOT_ERASED_FILE_AST_ID,
        };

        let builder = TopSubtreeBuilder::new(Delimiter {
            open: Span { range: TextRange::empty(TextSize::new(0)), anchor, ctx: make_ctx() },
            close: Span { range: TextRange::empty(TextSize::new(0)), anchor, ctx: make_ctx() },
            kind: DelimiterKind::Brace,
        });

        builder.build()
    }

    #[test]
    fn test_proc_macro_rpc_works() {
        for tt in [
            fixture_token_tree_top_many_none,
            fixture_token_tree_top_empty_none,
            fixture_token_tree_top_empty_brace,
        ] {
            for v in version::RUST_ANALYZER_SPAN_SUPPORT..=version::CURRENT_API_VERSION {
                let tt = tt();
                let mut span_data_table = Default::default();
                let task = ExpandMacro {
                    data: ExpandMacroData {
                        macro_body: FlatTree::from_subtree(tt.view(), v, &mut span_data_table),
                        macro_name: Default::default(),
                        attributes: None,
                        has_global_spans: ExpnGlobals {
                            serialize: true,
                            def_site: 0,
                            call_site: 0,
                            mixed_site: 0,
                        },
                        span_data_table: Vec::new(),
                    },
                    lib: Utf8PathBuf::from_path_buf(std::env::current_dir().unwrap()).unwrap(),
                    env: Default::default(),
                    current_dir: Default::default(),
                };

                let json = serde_json::to_string(&task).unwrap();
                // println!("{}", json);
                let back: ExpandMacro = serde_json::from_str(&json).unwrap();

                assert_eq!(
                    tt,
                    back.data.macro_body.to_subtree_resolved(v, &span_data_table),
                    "version: {v}"
                );
            }
        }
    }

    #[test]
    #[cfg(feature = "in-rust-tree")]
    fn test_proc_macro_rpc_works_ts() {
        for tt in [
            fixture_token_tree_top_many_none,
            fixture_token_tree_top_empty_none,
            fixture_token_tree_top_empty_brace,
        ] {
            let tt = tt();
            for v in version::RUST_ANALYZER_SPAN_SUPPORT..=version::CURRENT_API_VERSION {
                let mut span_data_table = Default::default();
                let flat_tree = FlatTree::from_subtree(tt.view(), v, &mut span_data_table);
                assert_eq!(
                    tt,
                    flat_tree.clone().to_subtree_resolved(v, &span_data_table),
                    "version: {v}"
                );
                let ts = flat_tree.to_tokenstream_resolved(v, &span_data_table, |a, b| a.cover(b));
                let call_site = *span_data_table.first().unwrap();
                let mut span_data_table = Default::default();
                assert_eq!(
                    tt,
                    FlatTree::from_tokenstream(ts.clone(), v, call_site, &mut span_data_table)
                        .to_subtree_resolved(v, &span_data_table),
                    "version: {v}, ts:\n{ts:#?}"
                );
            }
        }
    }
}