Skip to content

Commit c12dce0

Browse files
bors[bot]matklad
andauthored
Merge #1716
1716: make ast object safe r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents e055cfa + bbcca4f commit c12dce0

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

crates/ra_assists/src/ast_editor.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ pub struct AstEditor<N: AstNode> {
1919
}
2020

2121
impl<N: AstNode> AstEditor<N> {
22-
pub fn new(node: N) -> AstEditor<N> {
22+
pub fn new(node: N) -> AstEditor<N>
23+
where
24+
N: Clone,
25+
{
2326
AstEditor { original_ast: node.clone(), ast: node }
2427
}
2528

@@ -379,7 +382,7 @@ impl AstBuilder<ast::MatchArmList> {
379382

380383
fn ast_node_from_file_text<N: AstNode>(text: &str) -> N {
381384
let parse = SourceFile::parse(text);
382-
let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap().to_owned();
385+
let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap();
383386
res
384387
}
385388

crates/ra_syntax/src/ast.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,23 @@ pub use self::{
2525
/// conversion itself has zero runtime cost: ast and syntax nodes have exactly
2626
/// the same representation: a pointer to the tree root and a pointer to the
2727
/// node itself.
28-
pub trait AstNode: Clone {
29-
fn can_cast(kind: SyntaxKind) -> bool;
28+
pub trait AstNode {
29+
fn can_cast(kind: SyntaxKind) -> bool
30+
where
31+
Self: Sized;
3032

3133
fn cast(syntax: SyntaxNode) -> Option<Self>
3234
where
3335
Self: Sized;
36+
3437
fn syntax(&self) -> &SyntaxNode;
3538
}
3639

40+
#[test]
41+
fn assert_ast_is_object_safe() {
42+
fn _f(_: &dyn AstNode, _: &dyn NameOwner) {}
43+
}
44+
3745
/// Like `AstNode`, but wraps tokens rather than interior nodes.
3846
pub trait AstToken {
3947
fn cast(token: SyntaxToken) -> Option<Self>

0 commit comments

Comments
 (0)