Skip to content

Commit

Permalink
Allow where clauses involving types which don't include a type parame…
Browse files Browse the repository at this point in the history
…ter.

There isn't any semantic reason to disallow a `where` clause based on
the fact that it doesn't contain a type parameter.
  • Loading branch information
eefriedman committed Aug 24, 2015
1 parent 9f227ca commit db9e395
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 46 deletions.
46 changes: 0 additions & 46 deletions src/librustc_typeck/check/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
self.check_variances_for_type_defn(item, ast_generics);
}
ast::ItemTrait(_, _, _, ref items) => {
let trait_predicates =
ccx.tcx.lookup_predicates(local_def(item.id));
reject_non_type_param_bounds(ccx.tcx, item.span, &trait_predicates);
if ccx.tcx.trait_has_default_impl(local_def(item.id)) {
if !items.is_empty() {
wfcheck::error_380(ccx, item.span);
Expand All @@ -135,7 +132,6 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
let item_def_id = local_def(item.id);
let type_scheme = ccx.tcx.lookup_item_type(item_def_id);
let type_predicates = ccx.tcx.lookup_predicates(item_def_id);
reject_non_type_param_bounds(ccx.tcx, item.span, &type_predicates);
let param_env = ccx.tcx.construct_parameter_environment(item.span,
&type_scheme.generics,
&type_predicates,
Expand Down Expand Up @@ -367,44 +363,6 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
}
}

// Reject any predicates that do not involve a type parameter.
fn reject_non_type_param_bounds<'tcx>(tcx: &ty::ctxt<'tcx>,
span: Span,
predicates: &ty::GenericPredicates<'tcx>) {
for predicate in &predicates.predicates {
match predicate {
&ty::Predicate::Trait(ty::Binder(ref tr)) => {
let found_param = tr.input_types().iter()
.flat_map(|ty| ty.walk())
.any(is_ty_param);
if !found_param { report_bound_error(tcx, span, tr.self_ty() )}
}
&ty::Predicate::TypeOutlives(ty::Binder(ty::OutlivesPredicate(ty, _))) => {
let found_param = ty.walk().any(|t| is_ty_param(t));
if !found_param { report_bound_error(tcx, span, ty) }
}
_ => {}
};
}

fn report_bound_error<'t>(tcx: &ty::ctxt<'t>,
span: Span,
bounded_ty: ty::Ty<'t>) {
span_err!(tcx.sess, span, E0193,
"cannot bound type `{}`, where clause \
bounds may only be attached to types involving \
type parameters",
bounded_ty)
}

fn is_ty_param(ty: ty::Ty) -> bool {
match &ty.sty {
&ty::TyParam(_) => true,
_ => false
}
}
}

fn reject_shadowing_type_parameters<'tcx>(tcx: &ty::ctxt<'tcx>,
span: Span,
generics: &ty::Generics<'tcx>) {
Expand Down Expand Up @@ -445,10 +403,6 @@ impl<'ccx, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'ccx, 'tcx> {
if let ast::MethodTraitItem(_, None) = trait_item.node {
match self.tcx().impl_or_trait_item(local_def(trait_item.id)) {
ty::ImplOrTraitItem::MethodTraitItem(ty_method) => {
reject_non_type_param_bounds(
self.tcx(),
trait_item.span,
&ty_method.predicates);
reject_shadowing_type_parameters(
self.tcx(),
trait_item.span,
Expand Down
36 changes: 36 additions & 0 deletions src/test/compile-fail/where-clause-lifetimes-in-concrete-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2015 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(rustc_attrs)]

use std::cmp::PartialEq;

struct Input<'a> {
_bytes: &'a [u8],
}

impl <'a> PartialEq for Input<'a> {
fn eq(&self, _other: &Input<'a>) -> bool {
panic!()
}
}

struct Input2<'a> {
i: Input<'a>
}

impl<'a, 'b> PartialEq<Input2<'b>> for Input2<'a> where Input<'a> : PartialEq<Input<'b>> {
fn eq(&self, other: &Input2<'b>) -> bool {
self.i == other.i
}
}

#[rustc_error]
fn main() { } //~ ERROR compilation successful

0 comments on commit db9e395

Please sign in to comment.