Skip to content

Commit a0c9074

Browse files
cijiugechuanonrig
authored andcommitted
test(rust): add compile-fail doctest for escaped borrow
1 parent 0fed125 commit a0c9074

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

rust/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,22 @@ impl ExactSizeIterator for ExportIter<'_, '_> {}
329329
/// assert_eq!(analysis.exports_count(), 1);
330330
/// assert_eq!(analysis.export_name(0), Some("hello"));
331331
/// ```
332+
///
333+
/// Export names borrow from the analysis handle, so leaking them past
334+
/// `analysis`'s lifetime is rejected at compile time:
335+
///
336+
/// ```compile_fail
337+
/// use merve::parse_commonjs;
338+
///
339+
/// fn main() {
340+
/// let src = "exports['\\u0061\\u0062'] = 1;";
341+
/// let leaked: &str = {
342+
/// let analysis = parse_commonjs(src).unwrap();
343+
/// analysis.export_name(0).unwrap()
344+
/// }; // `analysis` is dropped here.
345+
/// let _ = leaked;
346+
/// }
347+
/// ```
332348
pub fn parse_commonjs(source: &str) -> Result<Analysis<'_>, LexerError> {
333349
if source.is_empty() {
334350
return Err(LexerError::EmptySource);

0 commit comments

Comments
 (0)