From 47b918d59aec86f0baf17b8de4bc0a86f7d77038 Mon Sep 17 00:00:00 2001 From: Tim Neumann Date: Sun, 25 Sep 2016 08:21:51 +0200 Subject: [PATCH] fix 36708 --- src/librustc_typeck/check/compare_method.rs | 46 +++++++++++-------- .../compile-fail/auxiliary/issue-36708.rs | 15 ++++++ src/test/compile-fail/issue-36708.rs | 23 ++++++++++ 3 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 src/test/compile-fail/auxiliary/issue-36708.rs create mode 100644 src/test/compile-fail/issue-36708.rs diff --git a/src/librustc_typeck/check/compare_method.rs b/src/librustc_typeck/check/compare_method.rs index 4a631493398..ffff05885ad 100644 --- a/src/librustc_typeck/check/compare_method.rs +++ b/src/librustc_typeck/check/compare_method.rs @@ -110,30 +110,40 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, _ => bug!("{:?} is not a method", impl_m) }; - struct_span_err!(tcx.sess, span, E0049, + let mut err = struct_span_err!(tcx.sess, span, E0049, "method `{}` has {} type parameter{} \ but its trait declaration has {} type parameter{}", trait_m.name, num_impl_m_type_params, if num_impl_m_type_params == 1 {""} else {"s"}, num_trait_m_type_params, - if num_trait_m_type_params == 1 {""} else {"s"}) - .span_label(trait_item_span.unwrap(), - &format!("expected {}", - &if num_trait_m_type_params != 1 { - format!("{} type parameters", - num_trait_m_type_params) - } else { - format!("{} type parameter", - num_trait_m_type_params) - })) - .span_label(span, &format!("found {}", - &if num_impl_m_type_params != 1 { - format!("{} type parameters", num_impl_m_type_params) - } else { - format!("1 type parameter") - })) - .emit(); + if num_trait_m_type_params == 1 {""} else {"s"}); + + let mut suffix = None; + + if let Some(span) = trait_item_span { + err.span_label(span, + &format!("expected {}", + &if num_trait_m_type_params != 1 { + format!("{} type parameters", num_trait_m_type_params) + } else { + format!("{} type parameter", num_trait_m_type_params) + })); + } else { + suffix = Some(format!(", expected {}", num_trait_m_type_params)); + } + + err.span_label(span, + &format!("found {}{}", + &if num_impl_m_type_params != 1 { + format!("{} type parameters", num_impl_m_type_params) + } else { + format!("1 type parameter") + }, + suffix.as_ref().map(|s| &s[..]).unwrap_or(""))); + + err.emit(); + return; } diff --git a/src/test/compile-fail/auxiliary/issue-36708.rs b/src/test/compile-fail/auxiliary/issue-36708.rs new file mode 100644 index 00000000000..e64e63a2139 --- /dev/null +++ b/src/test/compile-fail/auxiliary/issue-36708.rs @@ -0,0 +1,15 @@ +// Copyright 2016 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_type = "lib"] + +pub trait Foo { + fn foo(); +} diff --git a/src/test/compile-fail/issue-36708.rs b/src/test/compile-fail/issue-36708.rs new file mode 100644 index 00000000000..6146258fa8d --- /dev/null +++ b/src/test/compile-fail/issue-36708.rs @@ -0,0 +1,23 @@ +// Copyright 2016 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:issue-36708.rs + +extern crate issue_36708 as lib; + +struct Bar; + +impl lib::Foo for Bar { + fn foo() {} + //~^ ERROR E0049 + //~| NOTE found 1 type parameter, expected 0 +} + +fn main() {}