Skip to content

Commit 5b31bf8

Browse files
committed
Auto merge of #41668 - kennytm:fix-issue-41652, r=jonathandturner
Fix issue #41652 Fix issue #41652. Don't print anything in `render_source_line()` if no source code is given. (cc @jonathandturner #34789)
2 parents 2527f41 + 81bfdc8 commit 5b31bf8

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

src/librustc_errors/emitter.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,10 @@ impl EmitterWriter {
270270
line: &Line,
271271
width_offset: usize,
272272
code_offset: usize) -> Vec<(usize, Style)> {
273-
let source_string = file.get_line(line.line_index - 1)
274-
.unwrap_or("");
273+
let source_string = match file.get_line(line.line_index - 1) {
274+
Some(s) => s,
275+
None => return Vec::new(),
276+
};
275277

276278
let line_offset = buffer.num_lines();
277279

@@ -909,6 +911,11 @@ impl EmitterWriter {
909911

910912
// Print out the annotate source lines that correspond with the error
911913
for annotated_file in annotated_files {
914+
// we can't annotate anything if the source is unavailable.
915+
if annotated_file.file.src.is_none() {
916+
continue;
917+
}
918+
912919
// print out the span location and spacer before we print the annotated source
913920
// to do this, we need to know if this span will be primary
914921
let is_primary = primary_lo.file.name == annotated_file.file.name;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub trait Tr {
12+
// Note: The function needs to be declared over multiple lines to reproduce
13+
// the crash. DO NOT reformat.
14+
fn f()
15+
where Self: Sized;
16+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:issue_41652_b.rs
12+
13+
extern crate issue_41652_b;
14+
15+
struct S;
16+
17+
impl issue_41652_b::Tr for S {
18+
fn f() {
19+
3.f()
20+
//~^ ERROR no method named `f` found for type `{integer}` in the current scope
21+
//~| NOTE found the following associated functions
22+
//~| NOTE candidate #1 is defined in the trait `issue_41652_b::Tr`
23+
}
24+
}
25+
26+
fn main() {}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: no method named `f` found for type `{integer}` in the current scope
2+
--> $DIR/issue_41652.rs:19:11
3+
|
4+
19 | 3.f()
5+
| ^
6+
|
7+
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
8+
note: candidate #1 is defined in the trait `issue_41652_b::Tr`
9+
= help: to disambiguate the method call, write `issue_41652_b::Tr::f(3)` instead
10+
11+
error: aborting due to previous error
12+

0 commit comments

Comments
 (0)