Skip to content

Commit e2b7458

Browse files
authored
Rollup merge of #48020 - RalfJung:type-alias-bounds, r=petrochenkov
Warn about more ignored bounds in type aliases It seems that all bounds in type aliases are entirely ignored, not just type bounds. This extends the warning appropriately. I assume this should be made a hard error with the next epoch? I can't see any reason to accept these programs. (And suddenly enforcing these type bounds would be a breaking change.)
2 parents 993322e + ac183f8 commit e2b7458

File tree

5 files changed

+68
-23
lines changed

5 files changed

+68
-23
lines changed

src/librustc_typeck/collect.rs

+15-21
Original file line numberDiff line numberDiff line change
@@ -355,41 +355,35 @@ fn is_param<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
355355
}
356356
}
357357

358-
fn ensure_no_ty_param_bounds(tcx: TyCtxt,
359-
span: Span,
360-
generics: &hir::Generics,
361-
thing: &'static str) {
358+
fn ensure_no_param_bounds(tcx: TyCtxt,
359+
span: Span,
360+
generics: &hir::Generics,
361+
thing: &'static str) {
362362
let mut warn = false;
363363

364364
for ty_param in generics.ty_params() {
365-
for bound in ty_param.bounds.iter() {
366-
match *bound {
367-
hir::TraitTyParamBound(..) => {
368-
warn = true;
369-
}
370-
hir::RegionTyParamBound(..) => { }
371-
}
365+
if !ty_param.bounds.is_empty() {
366+
warn = true;
372367
}
373368
}
374369

375-
for predicate in generics.where_clause.predicates.iter() {
376-
match *predicate {
377-
hir::WherePredicate::BoundPredicate(..) => {
378-
warn = true;
379-
}
380-
hir::WherePredicate::RegionPredicate(..) => { }
381-
hir::WherePredicate::EqPredicate(..) => { }
370+
for lft_param in generics.lifetimes() {
371+
if !lft_param.bounds.is_empty() {
372+
warn = true;
382373
}
383374
}
384375

376+
if !generics.where_clause.predicates.is_empty() {
377+
warn = true;
378+
}
379+
385380
if warn {
386381
// According to accepted RFC #XXX, we should
387382
// eventually accept these, but it will not be
388383
// part of this PR. Still, convert to warning to
389384
// make bootstrapping easier.
390385
span_warn!(tcx.sess, span, E0122,
391-
"trait bounds are not (yet) enforced \
392-
in {} definitions",
386+
"generic bounds are ignored in {}",
393387
thing);
394388
}
395389
}
@@ -455,7 +449,7 @@ fn convert_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_id: ast::NodeId) {
455449
}
456450
},
457451
hir::ItemTy(_, ref generics) => {
458-
ensure_no_ty_param_bounds(tcx, it.span, generics, "type");
452+
ensure_no_param_bounds(tcx, it.span, generics, "type aliases");
459453
tcx.generics_of(def_id);
460454
tcx.type_of(def_id);
461455
tcx.predicates_of(def_id);

src/test/compile-fail/dst-bad-assign-3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#![feature(unsized_tuple_coercion)]
1414

1515
type Fat<T: ?Sized> = (isize, &'static str, T);
16-
//~^ WARNING trait bounds are not (yet) enforced
16+
//~^ WARNING bounds are ignored
1717

1818
#[derive(PartialEq,Eq)]
1919
struct Bar;

src/test/compile-fail/private-in-public-warn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ mod traits {
5858
pub trait PubTr {}
5959

6060
pub type Alias<T: PrivTr> = T; //~ ERROR private trait `traits::PrivTr` in public interface
61-
//~^ WARN trait bounds are not (yet) enforced in type definitions
61+
//~^ WARN bounds are ignored in type aliases
6262
//~| WARNING hard error
6363
pub trait Tr1: PrivTr {} //~ ERROR private trait `traits::PrivTr` in public interface
6464
//~^ WARNING hard error

src/test/ui/param-bounds-ignored.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2014 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+
// must-compile-successfully
12+
13+
use std::rc::Rc;
14+
15+
type SVec<T: Send> = Vec<T>;
16+
type VVec<'b, 'a: 'b> = Vec<&'a i32>;
17+
type WVec<'b, T: 'b> = Vec<T>;
18+
19+
fn foo<'a>(y: &'a i32) {
20+
// If the bounds above would matter, the code below would be rejected.
21+
let mut x : SVec<_> = Vec::new();
22+
x.push(Rc::new(42));
23+
24+
let mut x : VVec<'static, 'a> = Vec::new();
25+
x.push(y);
26+
27+
let mut x : WVec<'static, & 'a i32> = Vec::new();
28+
x.push(y);
29+
}
30+
31+
fn main() {
32+
foo(&42);
33+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
warning[E0122]: generic bounds are ignored in type aliases
2+
--> $DIR/param-bounds-ignored.rs:15:1
3+
|
4+
15 | type SVec<T: Send> = Vec<T>;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
warning[E0122]: generic bounds are ignored in type aliases
8+
--> $DIR/param-bounds-ignored.rs:16:1
9+
|
10+
16 | type VVec<'b, 'a: 'b> = Vec<&'a i32>;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
warning[E0122]: generic bounds are ignored in type aliases
14+
--> $DIR/param-bounds-ignored.rs:17:1
15+
|
16+
17 | type WVec<'b, T: 'b> = Vec<T>;
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+

0 commit comments

Comments
 (0)