From 5cbf9aedb806af63cc6ad80b19e598c2e0a10629 Mon Sep 17 00:00:00 2001
From: Guillaume Gomez <guillaume1.gomez@gmail.com>
Date: Tue, 20 Feb 2018 20:30:29 +0100
Subject: [PATCH] Fix rustdoc test ICE

---
 src/librustdoc/html/markdown.rs | 27 +++++++++++++++------------
 src/test/rustdoc/issue-48377.rs | 23 +++++++++++++++++++++++
 2 files changed, 38 insertions(+), 12 deletions(-)
 create mode 100644 src/test/rustdoc/issue-48377.rs

diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs
index fedd802ce55..b32acfe7f83 100644
--- a/src/librustdoc/html/markdown.rs
+++ b/src/librustdoc/html/markdown.rs
@@ -471,18 +471,21 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Sp
                         break 'main;
                     }
                 }
-                let offset = offset.unwrap_or(0);
-                let lines = test_s.lines().map(|l| map_line(l).for_code());
-                let text = lines.collect::<Vec<&str>>().join("\n");
-                nb_lines += doc[prev_offset..offset].lines().count();
-                let line = tests.get_line() + (nb_lines - 1);
-                let filename = tests.get_filename();
-                tests.add_test(text.to_owned(),
-                               block_info.should_panic, block_info.no_run,
-                               block_info.ignore, block_info.test_harness,
-                               block_info.compile_fail, block_info.error_codes,
-                               line, filename, block_info.allow_fail);
-                prev_offset = offset;
+                if let Some(offset) = offset {
+                    let lines = test_s.lines().map(|l| map_line(l).for_code());
+                    let text = lines.collect::<Vec<&str>>().join("\n");
+                    nb_lines += doc[prev_offset..offset].lines().count();
+                    let line = tests.get_line() + (nb_lines - 1);
+                    let filename = tests.get_filename();
+                    tests.add_test(text.to_owned(),
+                                   block_info.should_panic, block_info.no_run,
+                                   block_info.ignore, block_info.test_harness,
+                                   block_info.compile_fail, block_info.error_codes,
+                                   line, filename, block_info.allow_fail);
+                    prev_offset = offset;
+                } else {
+                    break;
+                }
             }
             Event::Start(Tag::Header(level)) => {
                 register_header = Some(level as u32);
diff --git a/src/test/rustdoc/issue-48377.rs b/src/test/rustdoc/issue-48377.rs
new file mode 100644
index 00000000000..d098b1a5b02
--- /dev/null
+++ b/src/test/rustdoc/issue-48377.rs
@@ -0,0 +1,23 @@
+// Copyright 2018 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.
+
+// compile-flags:--test
+
+//! This is a doc comment
+//!
+//! ```rust
+//! fn main() {}
+//! ```
+//!
+//! With a trailing code fence
+//! ```
+
+/// Some foo function
+pub fn foo() {}