Skip to content

Commit a333677

Browse files
committed
rustc_typeck: check well-formedness of type aliases.
1 parent f3290cd commit a333677

39 files changed

+350
-95
lines changed

src/Cargo.lock

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
250250

251251
[[package]]
252252
name = "chalk-engine"
253-
version = "0.7.0"
253+
version = "0.7.2"
254254
source = "registry+https://github.com/rust-lang/crates.io-index"
255255
dependencies = [
256256
"chalk-macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -643,6 +643,14 @@ dependencies = [
643643
"log 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
644644
]
645645

646+
[[package]]
647+
name = "ena"
648+
version = "0.10.1"
649+
source = "registry+https://github.com/rust-lang/crates.io-index"
650+
dependencies = [
651+
"log 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
652+
]
653+
646654
[[package]]
647655
name = "env_logger"
648656
version = "0.5.12"
@@ -1864,7 +1872,7 @@ dependencies = [
18641872
"backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
18651873
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
18661874
"byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
1867-
"chalk-engine 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
1875+
"chalk-engine 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
18681876
"flate2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
18691877
"fmt_macros 0.0.0",
18701878
"graphviz 0.0.0",
@@ -2130,7 +2138,7 @@ name = "rustc_data_structures"
21302138
version = "0.0.0"
21312139
dependencies = [
21322140
"cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
2133-
"ena 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
2141+
"ena 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
21342142
"log 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
21352143
"parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
21362144
"parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2392,7 +2400,7 @@ name = "rustc_traits"
23922400
version = "0.0.0"
23932401
dependencies = [
23942402
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
2395-
"chalk-engine 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
2403+
"chalk-engine 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
23962404
"graphviz 0.0.0",
23972405
"log 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
23982406
"rustc 0.0.0",
@@ -3146,7 +3154,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
31463154
"checksum cargo_metadata 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d6809b327f87369e6f3651efd2c5a96c49847a3ed2559477ecba79014751ee1"
31473155
"checksum cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "f159dfd43363c4d08055a07703eb7a3406b0dac4d0584d96965a3262db3c9d16"
31483156
"checksum cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e7bb64a8ebb0d856483e1e682ea3422f883c5f5615a90d51a2c82fe87fdd3"
3149-
"checksum chalk-engine 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "25ce2f28f55ed544a2a3756b7acf41dd7d6f27acffb2086439950925506af7d0"
3157+
"checksum chalk-engine 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "217042da49c0c0655bbc4aa9f9bed20c7a599429faddc9b2ef3bd09a2769d3ef"
31503158
"checksum chalk-macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "295635afd6853aa9f20baeb7f0204862440c0fe994c5a253d5f479dac41d047e"
31513159
"checksum chrono 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "6962c635d530328acc53ac6a955e83093fedc91c5809dfac1fa60fa470830a37"
31523160
"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e"
@@ -3176,6 +3184,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
31763184
"checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
31773185
"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
31783186
"checksum elasticlunr-rs 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4837d77a1e157489a3933b743fd774ae75074e0e390b2b7f071530048a0d87ee"
3187+
"checksum ena 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "25b4e5febb25f08c49f1b07dc33a182729a6b21edfb562b5aef95f78e0dbe5bb"
31793188
"checksum ena 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "88dc8393b3c7352f94092497f6b52019643e493b6b890eb417cdb7c46117e621"
31803189
"checksum env_logger 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)" = "f4d7e69c283751083d53d01eac767407343b8b69c4bd70058e08adc2637cb257"
31813190
"checksum environment 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f4b14e20978669064c33b4c1e0fb4083412e40fe56cbea2eae80fd7591503ee"

src/bootstrap/bin/rustc.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ fn main() {
304304
{
305305
cmd.arg("-Dwarnings");
306306
cmd.arg("-Dbare_trait_objects");
307+
if !(stage == "0" || target.is_none() && version.is_none()) {
308+
// HACK(eddyb) allow bootstrapping while we're testing with
309+
// the lint on `deny` by default. Remove before merging.
310+
cmd.arg("-Atype_alias_missing_bounds");
311+
}
307312
}
308313

309314
if verbose > 1 {

src/libgraphviz/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,10 @@ impl<'a> LabelText<'a> {
574574
}
575575
}
576576

577-
pub type Nodes<'a,N> = Cow<'a,[N]>;
578-
pub type Edges<'a,E> = Cow<'a,[E]>;
577+
#[allow(type_alias_bounds)]
578+
pub type Nodes<'a,N: Clone> = Cow<'a,[N]>;
579+
#[allow(type_alias_bounds)]
580+
pub type Edges<'a,E: Clone> = Cow<'a,[E]>;
579581

580582
// (The type parameters in GraphWalk should be associated items,
581583
// when/if Rust supports such.)

src/librustc/infer/canonical/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ pub struct QueryResult<'tcx, R> {
128128
pub value: R,
129129
}
130130

131-
pub type Canonicalized<'gcx, V> = Canonical<'gcx, <V as Lift<'gcx>>::Lifted>;
131+
#[allow(type_alias_bounds)]
132+
pub type Canonicalized<'gcx, V: Lift<'gcx>> = Canonical<'gcx, V::Lifted>;
132133

133-
pub type CanonicalizedQueryResult<'gcx, T> =
134-
Lrc<Canonical<'gcx, QueryResult<'gcx, <T as Lift<'gcx>>::Lifted>>>;
134+
#[allow(type_alias_bounds)]
135+
pub type CanonicalizedQueryResult<'gcx, T: Lift<'gcx>> =
136+
Lrc<Canonical<'gcx, QueryResult<'gcx, T::Lifted>>>;
135137

136138
/// Indicates whether or not we were able to prove the query to be
137139
/// true.

src/librustc/lint/builtin.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,12 @@ declare_lint! {
338338
cannot be referred to by absolute paths"
339339
}
340340

341+
declare_lint! {
342+
pub TYPE_ALIAS_MISSING_BOUNDS,
343+
Deny,
344+
"type aliases missing bounds required by the type being aliased, are now deprecated"
345+
}
346+
341347
/// Some lints that are buffered from `libsyntax`. See `syntax::early_buffered_lints`.
342348
pub mod parser {
343349
declare_lint! {
@@ -406,6 +412,7 @@ impl LintPass for HardwiredLints {
406412
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
407413
MACRO_USE_EXTERN_CRATE,
408414
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
415+
TYPE_ALIAS_MISSING_BOUNDS,
409416
parser::QUESTION_MARK_MACRO_SEP,
410417
)
411418
}

src/librustc/traits/error_reporting.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
584584
ObligationCauseCode::ImplDerivedObligation(data) => {
585585
self.get_lint_from_cause_code(&data.parent_code)
586586
}
587+
&ObligationCauseCode::TypeAliasMissingBound(id) => {
588+
if self.tcx.sess.rust_2018() {
589+
// Error since Rust 2018.
590+
None
591+
} else {
592+
Some((::lint::builtin::TYPE_ALIAS_MISSING_BOUNDS, id))
593+
}
594+
}
587595
_ => None,
588596
}
589597
}
@@ -1612,6 +1620,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
16121620
);
16131621
}
16141622
}
1623+
ObligationCauseCode::TypeAliasMissingBound(_) => {
1624+
err.help("missing bounds in type aliases were previously allowed");
1625+
if !self.tcx.sess.rust_2018() {
1626+
err.help("this is a hard error in Rust 2018");
1627+
}
1628+
}
16151629
}
16161630
}
16171631

src/librustc/traits/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ pub enum ObligationCauseCode<'tcx> {
251251

252252
/// #[feature(trivial_bounds)] is not enabled
253253
TrivialBound,
254+
255+
/// `type` alias is missing bounds required by the type being aliased
256+
/// (lint in Rust 2015, error since Rust 2018).
257+
TypeAliasMissingBound(ast::NodeId),
254258
}
255259

256260
#[derive(Clone, Debug, PartialEq, Eq, Hash)]

src/librustc/traits/structural_impls.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
238238
super::MethodReceiver => Some(super::MethodReceiver),
239239
super::BlockTailExpression(id) => Some(super::BlockTailExpression(id)),
240240
super::TrivialBound => Some(super::TrivialBound),
241+
super::ObligationCauseCode::TypeAliasMissingBound(id) => {
242+
Some(super::ObligationCauseCode::TypeAliasMissingBound(id))
243+
}
241244
}
242245
}
243246
}

src/librustc/ty/wf.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ enum Elaborate {
140140

141141
impl<'a, 'gcx, 'tcx> WfPredicates<'a, 'gcx, 'tcx> {
142142
fn cause(&mut self, code: traits::ObligationCauseCode<'tcx>) -> traits::ObligationCause<'tcx> {
143+
let code = match self.parent_cause.code {
144+
traits::ObligationCauseCode::TypeAliasMissingBound(_) => {
145+
// FIXME(eddyb) We're not chaining obligation causes here,
146+
// so in the case of cause codes that turn errors into lints,
147+
// we have to replace our cause `code` with the parent one.
148+
self.parent_cause.code.clone()
149+
}
150+
_ => code,
151+
};
143152
traits::ObligationCause::new(self.parent_cause.span, self.parent_cause.body_id, code)
144153
}
145154

src/librustc_data_structures/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ path = "lib.rs"
99
crate-type = ["dylib"]
1010

1111
[dependencies]
12-
ena = "0.9.3"
12+
ena = "0.10.1"
1313
log = "0.4"
1414
rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }
1515
serialize = { path = "../libserialize" }

src/librustc_data_structures/indexed_vec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,8 @@ impl<I: Idx, T: fmt::Debug> fmt::Debug for IndexVec<I, T> {
486486
}
487487
}
488488

489-
pub type Enumerated<I, J> = iter::Map<iter::Enumerate<J>, IntoIdx<I>>;
489+
#[allow(type_alias_bounds)]
490+
pub type Enumerated<I: Idx, J> = iter::Map<iter::Enumerate<J>, IntoIdx<I>>;
490491

491492
impl<I: Idx, T> IndexVec<I, T> {
492493
#[inline]

src/librustc_mir/util/liveness.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ use std::path::{Path, PathBuf};
4646
use transform::MirSource;
4747
use util::pretty::{dump_enabled, write_basic_block, write_mir_intro};
4848

49-
pub type LiveVarSet<V> = BitSet<V>;
49+
#[allow(type_alias_bounds)]
50+
pub type LiveVarSet<V: Idx> = BitSet<V>;
5051

5152
/// This gives the result of the liveness analysis at the boundary of
5253
/// basic blocks.

src/librustc_typeck/check/wfcheck.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ pub fn check_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: Def
129129
vec![] // no implied bounds in a static/const
130130
});
131131
}
132+
hir::ItemKind::Ty(ref hir_ty, _) => {
133+
let code = ObligationCauseCode::TypeAliasMissingBound(item.id);
134+
for_item(tcx, item).with_fcx(code.clone(), |fcx, _this| {
135+
let cause = traits::ObligationCause::new(hir_ty.span, fcx.body_id, code.clone());
136+
137+
let def_id = fcx.tcx.hir.local_def_id(item.id);
138+
let ty = fcx.tcx.type_of(def_id);
139+
let item_ty = fcx.inh.normalize_associated_types_in(cause, fcx.param_env, &ty);
140+
141+
fcx.register_wf_obligation(item_ty, hir_ty.span, code.clone());
142+
143+
check_where_clauses(tcx, fcx, item.span, def_id, None, code);
144+
145+
vec![item_ty]
146+
});
147+
}
132148
hir::ItemKind::Struct(ref struct_def, ref ast_generics) => {
133149
check_type_defn(tcx, item, false, |fcx| {
134150
vec![fcx.non_enum_variant(struct_def)]

src/test/rustdoc/assoc-item-cast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ pub trait AsExpression<T> {
2222
}
2323

2424
// @has foo/type.AsExprOf.html
25-
// @has - '//*[@class="rust typedef"]' 'type AsExprOf<Item, Type> = <Item as AsExpression<Type>>::Expression;'
26-
pub type AsExprOf<Item, Type> = <Item as AsExpression<Type>>::Expression;
25+
// @has - '//*[@class="rust typedef"]' 'type AsExprOf<Item: AsExpression<Type>, Type> = <Item as AsExpression<Type>>::Expression;'
26+
pub type AsExprOf<Item: AsExpression<Type>, Type> = <Item as AsExpression<Type>>::Expression;

src/test/rustdoc/const-evalutation-ice.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ pub struct S {
1717
s: Cell<usize>
1818
}
1919

20+
#[allow(type_alias_missing_bounds)] // HACK(eddyb) remove before merge
2021
pub type _S = [usize; 0 - (mem::size_of::<S>() != 4) as usize];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 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+
// compile-pass
12+
#![warn(const_err)]
13+
14+
#![crate_type = "lib"]
15+
16+
pub const Z: u32 = 0 - 1;
17+
//~^ WARN this constant cannot be used
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: this constant cannot be used
2+
--> $DIR/pub_const_err-warn.rs:16:1
3+
|
4+
LL | pub const Z: u32 = 0 - 1;
5+
| ^^^^^^^^^^^^^^^^^^^-----^
6+
| |
7+
| attempt to subtract with overflow
8+
|
9+
note: lint level defined here
10+
--> $DIR/pub_const_err-warn.rs:12:9
11+
|
12+
LL | #![warn(const_err)]
13+
| ^^^^^^^^^
14+

src/test/ui/consts/const-eval/pub_const_err.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-pass
12-
#![warn(const_err)]
13-
1411
#![crate_type = "lib"]
1512

16-
pub const Z: u32 = 0 - 1;
17-
//~^ WARN this constant cannot be used
18-
1913
pub type Foo = [i32; 0 - 1];
20-
//~^ WARN attempt to subtract with overflow
21-
//~| WARN this array length cannot be used
14+
//~^ ERROR could not evaluate constant expression
15+
//~| attempt to subtract with overflow
Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
warning: this constant cannot be used
2-
--> $DIR/pub_const_err.rs:16:1
3-
|
4-
LL | pub const Z: u32 = 0 - 1;
5-
| ^^^^^^^^^^^^^^^^^^^-----^
6-
| |
7-
| attempt to subtract with overflow
8-
|
9-
note: lint level defined here
10-
--> $DIR/pub_const_err.rs:12:9
11-
|
12-
LL | #![warn(const_err)]
13-
| ^^^^^^^^^
14-
15-
warning: attempt to subtract with overflow
16-
--> $DIR/pub_const_err.rs:19:22
1+
error: attempt to subtract with overflow
2+
--> $DIR/pub_const_err.rs:13:22
173
|
184
LL | pub type Foo = [i32; 0 - 1];
195
| ^^^^^
6+
|
7+
= note: #[deny(const_err)] on by default
208

21-
warning: this array length cannot be used
22-
--> $DIR/pub_const_err.rs:19:22
9+
error: could not evaluate constant expression
10+
--> $DIR/pub_const_err.rs:13:16
2311
|
2412
LL | pub type Foo = [i32; 0 - 1];
25-
| ^^^^^ attempt to subtract with overflow
13+
| ^^^^^^-----^
14+
| |
15+
| attempt to subtract with overflow
16+
|
17+
= note: #[deny(type_alias_missing_bounds)] on by default
18+
= help: missing bounds in type aliases were previously allowed
19+
= help: this is a hard error in Rust 2018
20+
21+
error: aborting due to 2 previous errors
2622

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 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+
// compile-pass
12+
#![warn(const_err)]
13+
14+
pub const Z: u32 = 0 - 1;
15+
//~^ WARN this constant cannot be used
16+
17+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: this constant cannot be used
2+
--> $DIR/pub_const_err_bin-warn.rs:14:1
3+
|
4+
LL | pub const Z: u32 = 0 - 1;
5+
| ^^^^^^^^^^^^^^^^^^^-----^
6+
| |
7+
| attempt to subtract with overflow
8+
|
9+
note: lint level defined here
10+
--> $DIR/pub_const_err_bin-warn.rs:12:9
11+
|
12+
LL | #![warn(const_err)]
13+
| ^^^^^^^^^
14+

0 commit comments

Comments
 (0)