From 2a1dc1eff62313bfa37eef6a2681269bda5f8439 Mon Sep 17 00:00:00 2001 From: jsirs <44773712+jsirs@users.noreply.github.com> Date: Mon, 5 Nov 2018 14:33:43 +0200 Subject: [PATCH 1/5] Add test Add test for incompleately implemented add trait, see issue #31076 --- src/test/ui/issues/issue-31076.rs | 15 +++++++++++++++ src/test/ui/issues/issue-31076.stderr | 11 +++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/test/ui/issues/issue-31076.rs create mode 100644 src/test/ui/issues/issue-31076.stderr diff --git a/src/test/ui/issues/issue-31076.rs b/src/test/ui/issues/issue-31076.rs new file mode 100644 index 0000000000000..3d235720c3883 --- /dev/null +++ b/src/test/ui/issues/issue-31076.rs @@ -0,0 +1,15 @@ +#![feature(no_core, lang_items)] +#![no_core] + +#[lang="sized"] +trait Sized {} + +#[lang="add"] +trait Add {} + +impl Add for i32 {} + +fn main() { + let x = 5 + 6; + //~^ ERROR binary operation `+` cannot be applied to type `{integer}` +} diff --git a/src/test/ui/issues/issue-31076.stderr b/src/test/ui/issues/issue-31076.stderr new file mode 100644 index 0000000000000..a667034bd8ce1 --- /dev/null +++ b/src/test/ui/issues/issue-31076.stderr @@ -0,0 +1,11 @@ +error[E0369]: binary operation `+` cannot be applied to type `{integer}` + --> $DIR/typeck-issue-31076-correct-trait-impl.rs:13:13 + | +LL | let x = 5 + 6; + | ^^^^^ + | + = note: an implementation of `std::ops::Add` might be missing for `{integer}` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0369`. From 4a08333c4d2769cf8b64cc0829f13984acffe908 Mon Sep 17 00:00:00 2001 From: jsirs <44773712+jsirs@users.noreply.github.com> Date: Mon, 5 Nov 2018 14:37:36 +0200 Subject: [PATCH 2/5] self.associated_item can return none self.associated_item can return none, replace unwrap with '?' and bubble up None value instead of panicking --- src/librustc_typeck/check/method/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/method/mod.rs b/src/librustc_typeck/check/method/mod.rs index 11448750618e2..3fd61dbb97d94 100644 --- a/src/librustc_typeck/check/method/mod.rs +++ b/src/librustc_typeck/check/method/mod.rs @@ -290,7 +290,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { // type parameters or early-bound regions. let tcx = self.tcx; let method_item = - self.associated_item(trait_def_id, m_name, Namespace::Value).unwrap(); + self.associated_item(trait_def_id, m_name, Namespace::Value)?; let def_id = method_item.def_id; let generics = tcx.generics_of(def_id); assert_eq!(generics.params.len(), 0); From 9fb2e0618e3a718ae12cd45b5ee39f9076ff3e79 Mon Sep 17 00:00:00 2001 From: jsirs <44773712+jsirs@users.noreply.github.com> Date: Mon, 5 Nov 2018 15:54:10 +0200 Subject: [PATCH 3/5] update test to include concrete type (i32) --- src/test/ui/issues/issue-31076.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/ui/issues/issue-31076.rs b/src/test/ui/issues/issue-31076.rs index 3d235720c3883..e4531072e9be4 100644 --- a/src/test/ui/issues/issue-31076.rs +++ b/src/test/ui/issues/issue-31076.rs @@ -12,4 +12,6 @@ impl Add for i32 {} fn main() { let x = 5 + 6; //~^ ERROR binary operation `+` cannot be applied to type `{integer}` + let y = 5i32 + 6i32; + //~^ ERROR binary operation `+` cannot be applied to type `i32` } From 87c22f2b141018d9283d3956468c43106d8765aa Mon Sep 17 00:00:00 2001 From: jsirs <44773712+jsirs@users.noreply.github.com> Date: Mon, 5 Nov 2018 15:57:07 +0200 Subject: [PATCH 4/5] add test for i32, fix incorrect location --- src/test/ui/issues/issue-31076.stderr | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/test/ui/issues/issue-31076.stderr b/src/test/ui/issues/issue-31076.stderr index a667034bd8ce1..3a13f02d9f45f 100644 --- a/src/test/ui/issues/issue-31076.stderr +++ b/src/test/ui/issues/issue-31076.stderr @@ -1,11 +1,19 @@ error[E0369]: binary operation `+` cannot be applied to type `{integer}` - --> $DIR/typeck-issue-31076-correct-trait-impl.rs:13:13 + --> $DIR/issue-31076.rs:13:13 | LL | let x = 5 + 6; | ^^^^^ | = note: an implementation of `std::ops::Add` might be missing for `{integer}` -error: aborting due to previous error +error[E0369]: binary operation `+` cannot be applied to type `i32` + --> $DIR/issue-31076.rs:15:13 + | +LL | let y = 5i32 + 6i32; + | ^^^^^^^^^^^ + | + = note: an implementation of `std::ops::Add` might be missing for `i32` + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0369`. From 306397774de67c28a3408b4315552d6fc9bcf4f3 Mon Sep 17 00:00:00 2001 From: jsirs <44773712+jsirs@users.noreply.github.com> Date: Mon, 5 Nov 2018 17:30:01 +0200 Subject: [PATCH 5/5] add call to tcx.sess.delay_span_bug add call to tcx.sess.delay_span_bug before returning none to make sure error is presented to user --- src/librustc_typeck/check/method/mod.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/librustc_typeck/check/method/mod.rs b/src/librustc_typeck/check/method/mod.rs index 3fd61dbb97d94..637f3eaae9a6a 100644 --- a/src/librustc_typeck/check/method/mod.rs +++ b/src/librustc_typeck/check/method/mod.rs @@ -289,8 +289,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { // Trait must have a method named `m_name` and it should not have // type parameters or early-bound regions. let tcx = self.tcx; - let method_item = - self.associated_item(trait_def_id, m_name, Namespace::Value)?; + let method_item = match self.associated_item(trait_def_id, m_name, Namespace::Value) { + Some(method_item) => method_item, + None => { + tcx.sess.delay_span_bug(span, + "operator trait does not have corresponding operator method"); + return None; + } + }; let def_id = method_item.def_id; let generics = tcx.generics_of(def_id); assert_eq!(generics.params.len(), 0);