Skip to content

Commit e75a4d5

Browse files
redsun82aibaars
authored andcommitted
Rust: fix compilation errors
1 parent 782f823 commit e75a4d5

File tree

5 files changed

+42
-38
lines changed

5 files changed

+42
-38
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ repos:
99
- id: trailing-whitespace
1010
exclude: /test/.*$(?<!\.qlref)|.*\.patch$|.*\.qll?$
1111
- id: end-of-file-fixer
12-
exclude: /test/.*$(?<!\.qlref)|.*\.patch$|.*\.qll?$
12+
exclude: Cargo.lock$|/test/.*$(?<!\.qlref)|.*\.patch$|.*\.qll?$
1313

1414
- repo: https://github.com/pre-commit/mirrors-clang-format
1515
rev: v17.0.6

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: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl<'a> Translator<'a> {
216216
if let Some(semantics) = self.semantics.as_ref() {
217217
let file_range = semantics.original_range(node.syntax());
218218
let file_id = self.file_id?;
219-
if file_id.file_id(semantics.db) == file_range.file_id {
219+
if file_id == file_range.file_id {
220220
Some(file_range.range)
221221
} else {
222222
None
@@ -370,20 +370,18 @@ impl<'a> Translator<'a> {
370370
if let Some(value) = semantics
371371
.hir_file_for(expanded)
372372
.macro_file()
373-
.and_then(|macro_file| {
374-
semantics
375-
.db
376-
.parse_macro_expansion_error(macro_file.macro_call_id)
377-
})
373+
.and_then(|macro_call_id| semantics.db.parse_macro_expansion_error(macro_call_id))
378374
{
379375
if let Some(err) = &value.err {
380376
let error = err.render_to_string(semantics.db);
381-
382-
if err.span().anchor.file_id == semantics.hir_file_for(node.syntax()) {
377+
let hir_file_id = semantics.hir_file_for(node.syntax());
378+
if Some(err.span().anchor.file_id.file_id())
379+
== hir_file_id.file_id().map(|f| f.file_id(semantics.db))
380+
{
383381
let location = err.span().range
384382
+ semantics
385383
.db
386-
.ast_id_map(err.span().anchor.file_id.into())
384+
.ast_id_map(hir_file_id)
387385
.get_erased(err.span().anchor.ast_id)
388386
.text_range()
389387
.start();
@@ -435,10 +433,10 @@ impl<'a> Translator<'a> {
435433
.as_ref()
436434
.and_then(|s| s.expand_macro_call(mcall))
437435
{
438-
self.emit_macro_expansion_parse_errors(mcall, &expanded);
436+
self.emit_macro_expansion_parse_errors(mcall, &expanded.value);
439437
let expand_to = ra_ap_hir_expand::ExpandTo::from_call_site(mcall);
440438
let kind = expanded.kind();
441-
if let Some(value) = self.emit_expanded_as(expand_to, expanded) {
439+
if let Some(value) = self.emit_expanded_as(expand_to, expanded.value) {
442440
generated::MacroCall::emit_macro_call_expansion(
443441
label,
444442
value,
@@ -741,11 +739,11 @@ impl<'a> Translator<'a> {
741739
) {
742740
// work around a bug in rust-analyzer AST generation machinery
743741
// this code was inspired by rust-analyzer's own workaround for this:
744-
// https://github.com/rust-lang/rust-analyzer/blob/1f86729f29ea50e8491a1516422df4fd3d1277b0/crates/syntax/src/ast/node_ext.rs#L268-L277
745-
if item.l_angle_token().is_some() {
742+
// https://github.com/rust-lang/rust-analyzer/blob/a642aa8023be11d6bc027fc6a68c71c2f3fc7f72/crates/syntax/src/ast/node_ext.rs#L290-L297
743+
if let Some(anchor) = item.type_anchor() {
746744
// <T> or <T as Trait>
747745
// T is any TypeRef, Trait has to be a PathType
748-
let mut type_refs = item
746+
let mut type_refs = anchor
749747
.syntax()
750748
.children()
751749
.filter(|node| ast::Type::can_cast(node.kind()));
@@ -763,6 +761,13 @@ impl<'a> Translator<'a> {
763761
{
764762
generated::PathSegment::emit_trait_type_repr(label, t, &mut self.trap.writer)
765763
}
764+
// moreover as we're skipping emission of TypeAnchor, we need to attach its comments to
765+
// this path segment
766+
self.emit_tokens(
767+
&anchor,
768+
label.into(),
769+
anchor.syntax().children_with_tokens(),
770+
);
766771
}
767772
}
768773

@@ -821,8 +826,8 @@ impl<'a> Translator<'a> {
821826
let ExpandResult {
822827
value: expanded, ..
823828
} = self.semantics.and_then(|s| s.expand_attr_macro(node))?;
824-
self.emit_macro_expansion_parse_errors(node, &expanded);
825-
let macro_items = ast::MacroItems::cast(expanded).or_else(|| {
829+
self.emit_macro_expansion_parse_errors(node, &expanded.value);
830+
let macro_items = ast::MacroItems::cast(expanded.value).or_else(|| {
826831
let message = "attribute macro expansion cannot be cast to MacroItems".to_owned();
827832
let location = self.location_for_node(node);
828833
self.emit_diagnostic(

0 commit comments

Comments
 (0)