From 62989c1a0cf0840315becf2ba0a141b9b3a1c62b Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 30 May 2017 22:21:00 -0700 Subject: [PATCH] associated_consts: check trait obligations and regionck for associated consts Closes #41323 --- src/librustc_typeck/check/compare_method.rs | 10 ++++++- .../associated-const-generic-obligations.rs | 30 +++++++++++++++++++ .../associated-const-impl-wrong-lifetime.rs | 27 +++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/test/compile-fail/associated-const-generic-obligations.rs create mode 100644 src/test/compile-fail/associated-const-impl-wrong-lifetime.rs diff --git a/src/librustc_typeck/check/compare_method.rs b/src/librustc_typeck/check/compare_method.rs index 767cf8f48cf..3121f494850 100644 --- a/src/librustc_typeck/check/compare_method.rs +++ b/src/librustc_typeck/check/compare_method.rs @@ -787,6 +787,14 @@ pub fn compare_const_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, diag.emit(); } - // FIXME(#41323) Check the obligations in the fulfillment context. + // Check that all obligations are satisfied by the implementation's + // version. + if let Err(ref errors) = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx) { + infcx.report_fulfillment_errors(errors); + return; + } + + let fcx = FnCtxt::new(&inh, impl_c_node_id); + fcx.regionck_item(impl_c_node_id, impl_c_span, &[]); }); } diff --git a/src/test/compile-fail/associated-const-generic-obligations.rs b/src/test/compile-fail/associated-const-generic-obligations.rs new file mode 100644 index 00000000000..90afe8d7336 --- /dev/null +++ b/src/test/compile-fail/associated-const-generic-obligations.rs @@ -0,0 +1,30 @@ +// Copyright 2017 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. + +#![feature(associated_consts)] + +trait Foo { + type Out: Sized; +} + +impl Foo for String { + type Out = String; +} + +trait Bar: Foo { + const FROM: Self::Out; +} + +impl Bar for T { + const FROM: &'static str = "foo"; + //~^ ERROR the trait bound `T: Foo` is not satisfied [E0277] +} + +fn main() {} diff --git a/src/test/compile-fail/associated-const-impl-wrong-lifetime.rs b/src/test/compile-fail/associated-const-impl-wrong-lifetime.rs new file mode 100644 index 00000000000..834f3460694 --- /dev/null +++ b/src/test/compile-fail/associated-const-impl-wrong-lifetime.rs @@ -0,0 +1,27 @@ +// Copyright 2017 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. + +#![feature(associated_consts)] + +trait Foo { + const NAME: &'static str; +} + + +impl<'a> Foo for &'a () { +//~^ NOTE the lifetime 'a as defined + const NAME: &'a str = "unit"; + //~^ ERROR mismatched types [E0308] + //~| NOTE lifetime mismatch + //~| NOTE expected type `&'static str` + //~| NOTE ...does not necessarily outlive the static lifetime +} + +fn main() {}