about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-01-02 16:08:23 +0000
committerGitHub <noreply@github.com>2022-01-02 16:08:23 +0000
commit367cd5ce9b7392b0898d4ac625e7266a3df5292a (patch)
tree5520ba31048e657014651b0d462a4b6b4bfa0c69
parent6e3d135f7b56ad19589008b2da5b08c33e679bf1 (diff)
parent3836b195dd36fd41631570741378c2453dadf40b (diff)
downloadrust-367cd5ce9b7392b0898d4ac625e7266a3df5292a.tar.gz
rust-367cd5ce9b7392b0898d4ac625e7266a3df5292a.zip
Merge #11168
11168: minor: drop dead code r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
-rw-r--r--crates/hir_expand/src/db.rs2
-rw-r--r--crates/mbe/src/syntax_bridge.rs19
-rw-r--r--crates/stdx/src/non_empty_vec.rs34
3 files changed, 18 insertions, 37 deletions
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index 3369e3e5fed..c88b5639f06 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -215,6 +215,8 @@ fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNod
     match file_id.0 {
         HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
         HirFileIdRepr::MacroFile(macro_file) => {
+            // FIXME: Note how we convert from `Parse` to `SyntaxNode` here,
+            // forgetting about parse errors.
             db.parse_macro_expansion(macro_file).value.map(|(it, _)| it.syntax_node())
         }
     }
diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs
index 7d7807206f4..5d705582415 100644
--- a/crates/mbe/src/syntax_bridge.rs
+++ b/crates/mbe/src/syntax_bridge.rs
@@ -1,7 +1,7 @@
 //! Conversions between [`SyntaxNode`] and [`tt::TokenTree`].
 
 use rustc_hash::{FxHashMap, FxHashSet};
-use stdx::{never, non_empty_vec::NonEmptyVec};
+use stdx::non_empty_vec::NonEmptyVec;
 use syntax::{
     ast::{self, make::tokens::doc_comment},
     AstToken, Parse, PreorderWithTokens, SmolStr, SyntaxElement, SyntaxKind,
@@ -66,10 +66,6 @@ pub fn token_tree_to_syntax_node(
             parser::Step::Error { msg } => tree_sink.error(msg.to_string()),
         }
     }
-    if never!(tree_sink.roots.len() != 1) {
-        return Err(ExpandError::ConversionError);
-    }
-    //FIXME: would be cool to report errors
     let (parse, range_map) = tree_sink.finish();
     Ok((parse, range_map))
 }
@@ -284,7 +280,7 @@ fn convert_tokens<C: TokenConvertor>(conv: &mut C) -> tt::Subtree {
         parent.subtree.token_trees.extend(entry.subtree.token_trees);
     }
 
-    let subtree = stack.into_first().subtree;
+    let subtree = stack.into_last().subtree;
     if let [tt::TokenTree::Subtree(first)] = &*subtree.token_trees {
         first.clone()
     } else {
@@ -614,10 +610,6 @@ struct TtTreeSink<'a> {
     text_pos: TextSize,
     inner: SyntaxTreeBuilder,
     token_map: TokenMap,
-
-    // Number of roots
-    // Use for detect ill-form tree which is not single root
-    roots: smallvec::SmallVec<[usize; 1]>,
 }
 
 impl<'a> TtTreeSink<'a> {
@@ -628,7 +620,6 @@ impl<'a> TtTreeSink<'a> {
             open_delims: FxHashMap::default(),
             text_pos: 0.into(),
             inner: SyntaxTreeBuilder::default(),
-            roots: smallvec::SmallVec::new(),
             token_map: TokenMap::default(),
         }
     }
@@ -733,16 +724,10 @@ impl<'a> TtTreeSink<'a> {
 
     fn start_node(&mut self, kind: SyntaxKind) {
         self.inner.start_node(kind);
-
-        match self.roots.last_mut() {
-            None | Some(0) => self.roots.push(1),
-            Some(n) => *n += 1,
-        };
     }
 
     fn finish_node(&mut self) {
         self.inner.finish_node();
-        *self.roots.last_mut().unwrap() -= 1;
     }
 
     fn error(&mut self, error: String) {
diff --git a/crates/stdx/src/non_empty_vec.rs b/crates/stdx/src/non_empty_vec.rs
index 199ee18b94b..342194c7838 100644
--- a/crates/stdx/src/non_empty_vec.rs
+++ b/crates/stdx/src/non_empty_vec.rs
@@ -1,45 +1,39 @@
-//! A [`Vec`] that is guaranteed to at least contain one element.
+//! See [`NonEmptyVec`].
 
-pub struct NonEmptyVec<T>(Vec<T>);
+/// A [`Vec`] that is guaranteed to at least contain one element.
+pub struct NonEmptyVec<T> {
+    first: T,
+    rest: Vec<T>,
+}
 
 impl<T> NonEmptyVec<T> {
     #[inline]
-    pub fn new(initial: T) -> Self {
-        NonEmptyVec(vec![initial])
+    pub fn new(first: T) -> Self {
+        NonEmptyVec { first, rest: Vec::new() }
     }
 
     #[inline]
     pub fn last_mut(&mut self) -> &mut T {
-        match self.0.last_mut() {
-            Some(it) => it,
-            None => unreachable!(),
-        }
+        self.rest.last_mut().unwrap_or(&mut self.first)
     }
 
     #[inline]
     pub fn pop(&mut self) -> Option<T> {
-        if self.0.len() <= 1 {
-            None
-        } else {
-            self.0.pop()
-        }
+        self.rest.pop()
     }
 
     #[inline]
     pub fn push(&mut self, value: T) {
-        self.0.push(value)
+        self.rest.push(value)
     }
 
     #[inline]
     pub fn len(&self) -> usize {
-        self.0.len()
+        1 + self.rest.len()
     }
 
     #[inline]
-    pub fn into_first(mut self) -> T {
-        match self.0.pop() {
-            Some(it) => it,
-            None => unreachable!(),
-        }
+    pub fn into_last(mut self) -> T {
+        self.rest.pop().unwrap_or(self.first)
     }
 }