Skip to content

Commit

Permalink
Rollup merge of rust-lang#47210 - zackmdavis:the_3rd_of_2_hardest_pro…
Browse files Browse the repository at this point in the history
…blems_in_computer_science, r=QuietMisdreavus

fix the doc-comment-decoration-trimming edge-case rustdoc ICE

This `horizontal_trim` function strips the leading whitespace from
doc-comments that have a left-asterisk-margin:

```
  /**
   * You know what I mean—
   *
   * comments like this!
   */
```

The index of the column of asterisks is `i`, and if trimming is deemed
possible, we slice each line from `i+1` to the end of the line. But if, in
particular, `i` was 0 _and_ there was an empty line (as in the example
given in the reporting issue), we ended up panicking trying to slice an
empty string from 0+1 (== 1).

Let's tighten our check to say that we can't trim when `i` is even the same
as the length of the line, not just when it's greater. (Any such cases
would panic trying to slice `line` from `line.len()+1`.)

Resolves rust-lang#47197.
  • Loading branch information
kennytm authored Jan 8, 2018
2 parents b5392f5 + 3cfea33 commit 4a6f440
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libsyntax/parse/lexer/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
break;
}
}
if i > line.len() {
if i >= line.len() {
can_trim = false;
}
if !can_trim {
Expand Down
18 changes: 18 additions & 0 deletions src/test/rustdoc/issue-47197-blank-line-in-doc-block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// @has issue_47197_blank_line_in_doc_block/fn.whose_woods_these_are_i_think_i_know.html

/**
* snow
* ice
*/
pub fn whose_woods_these_are_i_think_i_know() {}

0 comments on commit 4a6f440

Please sign in to comment.