Skip to content

Commit 2d1d4d1

Browse files
authored
Auto merge of #36713 - TimNN:fix-36708, r=arielb1
don't unwrap non-local span Fixes #36708.
2 parents d6dc339 + 47b918d commit 2d1d4d1

File tree

3 files changed

+66
-18
lines changed

3 files changed

+66
-18
lines changed

src/librustc_typeck/check/compare_method.rs

+28-18
Original file line numberDiff line numberDiff line change
@@ -110,30 +110,40 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
110110
_ => bug!("{:?} is not a method", impl_m)
111111
};
112112

113-
struct_span_err!(tcx.sess, span, E0049,
113+
let mut err = struct_span_err!(tcx.sess, span, E0049,
114114
"method `{}` has {} type parameter{} \
115115
but its trait declaration has {} type parameter{}",
116116
trait_m.name,
117117
num_impl_m_type_params,
118118
if num_impl_m_type_params == 1 {""} else {"s"},
119119
num_trait_m_type_params,
120-
if num_trait_m_type_params == 1 {""} else {"s"})
121-
.span_label(trait_item_span.unwrap(),
122-
&format!("expected {}",
123-
&if num_trait_m_type_params != 1 {
124-
format!("{} type parameters",
125-
num_trait_m_type_params)
126-
} else {
127-
format!("{} type parameter",
128-
num_trait_m_type_params)
129-
}))
130-
.span_label(span, &format!("found {}",
131-
&if num_impl_m_type_params != 1 {
132-
format!("{} type parameters", num_impl_m_type_params)
133-
} else {
134-
format!("1 type parameter")
135-
}))
136-
.emit();
120+
if num_trait_m_type_params == 1 {""} else {"s"});
121+
122+
let mut suffix = None;
123+
124+
if let Some(span) = trait_item_span {
125+
err.span_label(span,
126+
&format!("expected {}",
127+
&if num_trait_m_type_params != 1 {
128+
format!("{} type parameters", num_trait_m_type_params)
129+
} else {
130+
format!("{} type parameter", num_trait_m_type_params)
131+
}));
132+
} else {
133+
suffix = Some(format!(", expected {}", num_trait_m_type_params));
134+
}
135+
136+
err.span_label(span,
137+
&format!("found {}{}",
138+
&if num_impl_m_type_params != 1 {
139+
format!("{} type parameters", num_impl_m_type_params)
140+
} else {
141+
format!("1 type parameter")
142+
},
143+
suffix.as_ref().map(|s| &s[..]).unwrap_or("")));
144+
145+
err.emit();
146+
137147
return;
138148
}
139149

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 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+
#![crate_type = "lib"]
12+
13+
pub trait Foo {
14+
fn foo();
15+
}

src/test/compile-fail/issue-36708.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2016 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-36708.rs
12+
13+
extern crate issue_36708 as lib;
14+
15+
struct Bar;
16+
17+
impl lib::Foo for Bar {
18+
fn foo<T>() {}
19+
//~^ ERROR E0049
20+
//~| NOTE found 1 type parameter, expected 0
21+
}
22+
23+
fn main() {}

0 commit comments

Comments
 (0)