Skip to content

Commit 02a9d4c

Browse files
committed
Rust: fix compilation errors
1 parent efd318d commit 02a9d4c

File tree

5 files changed

+42
-39
lines changed

5 files changed

+42
-39
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/extractor/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub struct Config {
5252
pub cargo_target: Option<String>,
5353
pub cargo_features: Vec<String>,
5454
pub cargo_cfg_overrides: Vec<String>,
55-
pub cargo_extra_env: FxHashMap<String, String>,
55+
pub cargo_extra_env: FxHashMap<String, Option<String>>,
5656
pub cargo_extra_args: Vec<String>,
5757
pub cargo_all_targets: bool,
5858
pub logging_flamegraph: Option<PathBuf>,

rust/extractor/src/config/deserialize.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use serde::Deserializer;
2-
use serde::de::{Error, Unexpected, Visitor};
2+
use serde::de::Visitor;
33
use std::collections::HashMap;
44
use std::fmt::Formatter;
55
use std::hash::BuildHasher;
@@ -36,23 +36,22 @@ impl<'de, T: From<String>> Visitor<'de> for VectorVisitor<T> {
3636
}
3737

3838
impl<'de, S: BuildHasher + Default> Visitor<'de> for MapVisitor<S> {
39-
type Value = HashMap<String, String, S>;
39+
type Value = HashMap<String, Option<String>, S>;
4040

4141
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
4242
formatter.write_str(
43-
"either a sequence, or a comma or newline separated string of key=value entries",
43+
"either a sequence, or a comma or newline separated string of key[=value] entries",
4444
)
4545
}
4646

4747
fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<Self::Value, E> {
48-
value
48+
Ok(value
4949
.split(['\n', ','])
50-
.map(|s| {
51-
s.split_once('=')
52-
.ok_or_else(|| E::custom(format!("key=value expected, found {s}")))
53-
.map(|(key, value)| (key.to_owned(), value.to_owned()))
50+
.map(|s| match s.split_once('=') {
51+
Some((key, value)) => (key.to_owned(), Some(value.to_owned())),
52+
None => (s.to_owned(), None),
5453
})
55-
.collect()
54+
.collect())
5655
}
5756

5857
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
@@ -61,10 +60,14 @@ impl<'de, S: BuildHasher + Default> Visitor<'de> for MapVisitor<S> {
6160
{
6261
let mut ret = HashMap::with_hasher(Default::default());
6362
while let Some(el) = seq.next_element::<String>()? {
64-
let (key, value) = el
65-
.split_once('=')
66-
.ok_or_else(|| A::Error::invalid_value(Unexpected::Str(&el), &self))?;
67-
ret.insert(key.to_owned(), value.to_owned());
63+
match el.split_once('=') {
64+
None => {
65+
ret.insert(el.to_owned(), None);
66+
}
67+
Some((key, value)) => {
68+
ret.insert(key.to_owned(), Some(value.to_owned()));
69+
}
70+
}
6871
}
6972
Ok(ret)
7073
}
@@ -83,7 +86,7 @@ pub(crate) fn deserialize_newline_or_comma_separated_vec<
8386
deserializer.deserialize_seq(VectorVisitor(PhantomData))
8487
}
8588

86-
/// deserialize into a map of `String`s to `String`s either of:
89+
/// deserialize into a map of `String`s to `Option<String>`s either of:
8790
/// * a sequence of elements serializable into `String`s, or
8891
/// * a single element serializable into `String`, then split on `,` and `\n`
8992
pub(crate) fn deserialize_newline_or_comma_separated_map<
@@ -92,6 +95,6 @@ pub(crate) fn deserialize_newline_or_comma_separated_map<
9295
S: BuildHasher + Default,
9396
>(
9497
deserializer: D,
95-
) -> Result<HashMap<String, String, S>, D::Error> {
98+
) -> Result<HashMap<String, Option<String>, S>, D::Error> {
9699
deserializer.deserialize_map(MapVisitor(PhantomData))
97100
}

rust/extractor/src/rust_analyzer.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ impl<'a> RustAnalyzer<'a> {
7777
let editioned_file_id = semantics
7878
.attach_first_edition(file_id)
7979
.ok_or("failed to determine rust edition")?;
80-
Ok((
81-
semantics,
82-
EditionedFileId::new(semantics.db, editioned_file_id),
83-
input,
84-
))
80+
Ok((semantics, editioned_file_id, input))
8581
}
8682
}
8783
}

rust/extractor/src/translate/base.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'a> Translator<'a> {
174174
if let Some(semantics) = self.semantics.as_ref() {
175175
let file_range = semantics.original_range(node.syntax());
176176
let file_id = self.file_id?;
177-
if file_id.file_id(semantics.db) == file_range.file_id {
177+
if file_id == file_range.file_id {
178178
Some(file_range.range)
179179
} else {
180180
None
@@ -298,20 +298,18 @@ impl<'a> Translator<'a> {
298298
if let Some(value) = semantics
299299
.hir_file_for(expanded)
300300
.macro_file()
301-
.and_then(|macro_file| {
302-
semantics
303-
.db
304-
.parse_macro_expansion_error(macro_file.macro_call_id)
305-
})
301+
.and_then(|macro_call_id| semantics.db.parse_macro_expansion_error(macro_call_id))
306302
{
307303
if let Some(err) = &value.err {
308304
let error = err.render_to_string(semantics.db);
309-
310-
if err.span().anchor.file_id == semantics.hir_file_for(node.syntax()) {
305+
let hir_file_id = semantics.hir_file_for(node.syntax());
306+
if Some(err.span().anchor.file_id.file_id())
307+
== hir_file_id.file_id().map(|f| f.file_id(semantics.db))
308+
{
311309
let location = err.span().range
312310
+ semantics
313311
.db
314-
.ast_id_map(err.span().anchor.file_id.into())
312+
.ast_id_map(hir_file_id)
315313
.get_erased(err.span().anchor.ast_id)
316314
.text_range()
317315
.start();
@@ -363,10 +361,10 @@ impl<'a> Translator<'a> {
363361
.as_ref()
364362
.and_then(|s| s.expand_macro_call(mcall))
365363
{
366-
self.emit_macro_expansion_parse_errors(mcall, &expanded);
364+
self.emit_macro_expansion_parse_errors(mcall, &expanded.value);
367365
let expand_to = ra_ap_hir_expand::ExpandTo::from_call_site(mcall);
368366
let kind = expanded.kind();
369-
if let Some(value) = self.emit_expanded_as(expand_to, expanded) {
367+
if let Some(value) = self.emit_expanded_as(expand_to, expanded.value) {
370368
generated::MacroCall::emit_macro_call_expansion(
371369
label,
372370
value,
@@ -669,11 +667,11 @@ impl<'a> Translator<'a> {
669667
) {
670668
// work around a bug in rust-analyzer AST generation machinery
671669
// this code was inspired by rust-analyzer's own workaround for this:
672-
// https://github.com/rust-lang/rust-analyzer/blob/1f86729f29ea50e8491a1516422df4fd3d1277b0/crates/syntax/src/ast/node_ext.rs#L268-L277
673-
if item.l_angle_token().is_some() {
670+
// https://github.com/rust-lang/rust-analyzer/blob/a642aa8023be11d6bc027fc6a68c71c2f3fc7f72/crates/syntax/src/ast/node_ext.rs#L290-L297
671+
if let Some(anchor) = item.type_anchor() {
674672
// <T> or <T as Trait>
675673
// T is any TypeRef, Trait has to be a PathType
676-
let mut type_refs = item
674+
let mut type_refs = anchor
677675
.syntax()
678676
.children()
679677
.filter(|node| ast::Type::can_cast(node.kind()));
@@ -691,6 +689,13 @@ impl<'a> Translator<'a> {
691689
{
692690
generated::PathSegment::emit_trait_type_repr(label, t, &mut self.trap.writer)
693691
}
692+
// moreover as we're skipping emission of TypeAnchor, we need to attach its comments to
693+
// this path segment
694+
self.emit_tokens(
695+
&anchor,
696+
label.into(),
697+
anchor.syntax().children_with_tokens(),
698+
);
694699
}
695700
}
696701

@@ -743,9 +748,9 @@ impl<'a> Translator<'a> {
743748
}
744749
let ExpandResult {
745750
value: expanded, ..
746-
} = semantics.expand_attr_macro(node)?;
747-
self.emit_macro_expansion_parse_errors(node, &expanded);
748-
let macro_items = ast::MacroItems::cast(expanded).or_else(|| {
751+
} = self.semantics.and_then(|s| s.expand_attr_macro(node))?;
752+
self.emit_macro_expansion_parse_errors(node, &expanded.value);
753+
let macro_items = ast::MacroItems::cast(expanded.value).or_else(|| {
749754
let message = "attribute macro expansion cannot be cast to MacroItems".to_owned();
750755
let location = self.location_for_node(node);
751756
self.emit_diagnostic(

0 commit comments

Comments
 (0)