@@ -1079,7 +1079,8 @@ thread_local! {
1079
1079
1080
1080
/// Creates an iterator over the captures in a query within the given range,
1081
1081
/// re-using a cursor from the pool if available.
1082
- fn query_captures < ' a , ' tree > (
1082
+ /// SAFETY: The `QueryCaptures` must be droped before the `QueryCursor` is dropped.
1083
+ unsafe fn query_captures < ' a , ' tree > (
1083
1084
query : & ' a Query ,
1084
1085
root : Node < ' tree > ,
1085
1086
source : RopeSlice < ' a > ,
@@ -1094,10 +1095,11 @@ fn query_captures<'a, 'tree>(
1094
1095
highlighter. cursors . pop ( ) . unwrap_or_default ( )
1095
1096
} ) ;
1096
1097
1098
+ // This is the unsafe line:
1097
1099
// The `captures` iterator borrows the `Tree` and the `QueryCursor`, which
1098
1100
// prevents them from being moved. But both of these values are really just
1099
1101
// pointers, so it's actually ok to move them.
1100
- let cursor_ref = unsafe { mem:: transmute :: < _ , & ' static mut QueryCursor > ( & mut cursor) } ;
1102
+ let cursor_ref = mem:: transmute :: < _ , & ' static mut QueryCursor > ( & mut cursor) ;
1101
1103
1102
1104
// if reusing cursors & no range this resets to whole range
1103
1105
cursor_ref. set_byte_range ( range. unwrap_or ( 0 ..usize:: MAX ) ) ;
@@ -1460,12 +1462,14 @@ impl Syntax {
1460
1462
. layers
1461
1463
. iter ( )
1462
1464
. filter_map ( |( _, layer) | {
1463
- let ( cursor, captures) = query_captures (
1464
- query_fn ( & layer. config ) ,
1465
- layer. tree ( ) . root_node ( ) ,
1466
- source,
1467
- range. clone ( ) ,
1468
- ) ;
1465
+ let ( cursor, captures) = unsafe {
1466
+ query_captures (
1467
+ query_fn ( & layer. config ) ,
1468
+ layer. tree ( ) . root_node ( ) ,
1469
+ source,
1470
+ range. clone ( ) ,
1471
+ )
1472
+ } ;
1469
1473
let mut captures = captures. peekable ( ) ;
1470
1474
1471
1475
// If there aren't any captures for this layer, skip the layer.
@@ -1497,12 +1501,14 @@ impl Syntax {
1497
1501
. filter_map ( |( _, layer) | {
1498
1502
// TODO: if range doesn't overlap layer range, skip it
1499
1503
1500
- let ( cursor, captures) = query_captures (
1501
- & layer. config . query ,
1502
- layer. tree ( ) . root_node ( ) ,
1503
- source,
1504
- range. clone ( ) ,
1505
- ) ;
1504
+ let ( cursor, captures) = unsafe {
1505
+ query_captures (
1506
+ & layer. config . query ,
1507
+ layer. tree ( ) . root_node ( ) ,
1508
+ source,
1509
+ range. clone ( ) ,
1510
+ )
1511
+ } ;
1506
1512
let mut captures = captures. peekable ( ) ;
1507
1513
1508
1514
// If there are no captures, skip the layer
0 commit comments