From 7d8360febd77ea7c358fb5963f593b5f5078bdd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 16 Mar 2020 13:21:58 -0700 Subject: [PATCH 1/5] Handle associated types in `const` context When in `const` context (like array lengths) we erase all substs. Because of this, we used to emit spurious suggestions to restrict type parameters when using associated types on them, even though that would never work. Instead, we now signal `const` contexts when evaluating traits and emit a more accurate error. --- src/librustc/query/mod.rs | 15 ++ src/librustc_infer/infer/mod.rs | 5 + src/librustc_mir/const_eval/eval_queries.rs | 4 +- .../traits/error_reporting/suggestions.rs | 6 + src/librustc_typeck/check/mod.rs | 52 +++++- ...iated-const-type-parameter-arrays-2.stderr | 6 +- ...ociated-const-type-parameter-arrays.stderr | 6 +- src/test/ui/closures/issue-52437.rs | 1 + src/test/ui/closures/issue-52437.stderr | 8 +- src/test/ui/consts/const-integer-bool-ops.rs | 27 ++- .../ui/consts/const-integer-bool-ops.stderr | 160 +++++++++++++++--- src/test/ui/consts/const-tup-index-span.rs | 2 +- .../ui/consts/const-tup-index-span.stderr | 11 +- src/test/ui/consts/issue-52432.rs | 1 + src/test/ui/consts/issue-52432.stderr | 12 +- .../ui/consts/too_generic_eval_ice.stderr | 8 +- src/test/ui/inference/inference_unstable.rs | 4 +- .../ui/inference/inference_unstable.stderr | 11 ++ src/test/ui/issues/issue-41394.rs | 1 + src/test/ui/issues/issue-41394.stderr | 12 +- src/test/ui/issues/issue-50688.rs | 1 + src/test/ui/issues/issue-50688.stderr | 11 +- src/test/ui/issues/issue-51714.rs | 3 + src/test/ui/issues/issue-51714.stderr | 28 ++- src/test/ui/issues/issue-54954.rs | 1 + src/test/ui/issues/issue-54954.stderr | 19 ++- 26 files changed, 337 insertions(+), 78 deletions(-) diff --git a/src/librustc/query/mod.rs b/src/librustc/query/mod.rs index 11e9acf3a3912..704f729f77e2c 100644 --- a/src/librustc/query/mod.rs +++ b/src/librustc/query/mod.rs @@ -437,6 +437,19 @@ rustc_queries! { desc { |tcx| "type-checking `{}`", tcx.def_path_str(key) } cache_on_disk_if { key.is_local() } } + + query const_typeck_tables_of(key: DefId) -> &'tcx ty::TypeckTables<'tcx> { + desc { |tcx| "type-checking `{}`", tcx.def_path_str(key) } + cache_on_disk_if { key.is_local() } + load_cached(tcx, id) { + let typeck_tables: Option> = tcx + .queries.on_disk_cache + .try_load_query_result(tcx, id); + + typeck_tables.map(|tables| &*tcx.arena.alloc(tables)) + } + } + query diagnostic_only_typeck_tables_of(key: DefId) -> &'tcx ty::TypeckTables<'tcx> { cache_on_disk_if { key.is_local() } load_cached(tcx, id) { @@ -458,6 +471,8 @@ rustc_queries! { TypeChecking { query has_typeck_tables(_: DefId) -> bool {} + query const_has_typeck_tables(_: DefId) -> bool {} + query coherent_trait(def_id: DefId) -> () { desc { |tcx| "coherence checking all impls of trait `{}`", tcx.def_path_str(def_id) } } diff --git a/src/librustc_infer/infer/mod.rs b/src/librustc_infer/infer/mod.rs index 9ae131c568d0d..e04ae624658dd 100644 --- a/src/librustc_infer/infer/mod.rs +++ b/src/librustc_infer/infer/mod.rs @@ -250,6 +250,10 @@ pub struct InferCtxt<'a, 'tcx> { /// This flag is true while there is an active snapshot. in_snapshot: Cell, + /// This flag is `true` if the current scope is in a `const` context, + /// like in an array length. Used exclusively to improve diagnostics. + pub in_const_context: Cell, + /// What is the innermost universe we have created? Starts out as /// `UniverseIndex::root()` but grows from there as we enter /// universal quantifiers. @@ -584,6 +588,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> { tainted_by_errors_flag: Cell::new(false), err_count_on_creation: tcx.sess.err_count(), in_snapshot: Cell::new(false), + in_const_context: Cell::new(false), skip_leak_check: Cell::new(false), universe: Cell::new(ty::UniverseIndex::ROOT), }) diff --git a/src/librustc_mir/const_eval/eval_queries.rs b/src/librustc_mir/const_eval/eval_queries.rs index ffbff00cf3760..767caa0a475b0 100644 --- a/src/librustc_mir/const_eval/eval_queries.rs +++ b/src/librustc_mir/const_eval/eval_queries.rs @@ -290,8 +290,8 @@ pub fn const_eval_raw_provider<'tcx>( let def_id = cid.instance.def.def_id(); if def_id.is_local() - && tcx.has_typeck_tables(def_id) - && tcx.typeck_tables_of(def_id).tainted_by_errors + && tcx.const_has_typeck_tables(def_id) + && tcx.const_typeck_tables_of(def_id).tainted_by_errors { return Err(ErrorHandled::Reported); } diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index 351e557d40b30..f60e6bf81379a 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -158,6 +158,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { ty::Projection(projection) => (false, Some(projection)), _ => return, }; + if self.in_const_context.get() { + err.note( + "associated types can't be referenced in `const` contexts, like array in length", + ); + return; + } let suggest_restriction = |generics: &hir::Generics<'_>, msg, err: &mut DiagnosticBuilder<'_>| { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 1975b24899960..52040529180e6 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -753,7 +753,12 @@ fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: DefId) { fn typeck_item_bodies(tcx: TyCtxt<'_>, crate_num: CrateNum) { debug_assert!(crate_num == LOCAL_CRATE); tcx.par_body_owners(|body_owner_def_id| { - tcx.ensure().typeck_tables_of(body_owner_def_id); + if let Some(hir::Node::AnonConst(_)) = tcx.hir().get_if_local(body_owner_def_id) { + // We don't want to incorrectly suggest constraining type parameters for array lengths. + tcx.ensure().const_typeck_tables_of(body_owner_def_id); + } else { + tcx.ensure().typeck_tables_of(body_owner_def_id); + } }); } @@ -774,8 +779,10 @@ pub fn provide(providers: &mut Providers<'_>) { *providers = Providers { typeck_item_bodies, typeck_tables_of, + const_typeck_tables_of, diagnostic_only_typeck_tables_of, has_typeck_tables, + const_has_typeck_tables, adt_destructor, used_trait_imports, check_item_well_formed, @@ -834,11 +841,23 @@ fn primary_body_of( } fn has_typeck_tables(tcx: TyCtxt<'_>, def_id: DefId) -> bool { + has_typeck_tables_inner(tcx, def_id, false) +} + +fn const_has_typeck_tables(tcx: TyCtxt<'_>, def_id: DefId) -> bool { + has_typeck_tables_inner(tcx, def_id, true) +} + +fn has_typeck_tables_inner(tcx: TyCtxt<'_>, def_id: DefId, in_const_context: bool) -> bool { // Closures' tables come from their outermost function, // as they are part of the same "inference environment". let outer_def_id = tcx.closure_base_def_id(def_id); if outer_def_id != def_id { - return tcx.has_typeck_tables(outer_def_id); + if in_const_context { + return tcx.const_has_typeck_tables(outer_def_id); + } else { + return tcx.has_typeck_tables(outer_def_id); + } } if let Some(id) = tcx.hir().as_local_hir_id(def_id) { @@ -849,7 +868,13 @@ fn has_typeck_tables(tcx: TyCtxt<'_>, def_id: DefId) -> bool { } fn used_trait_imports(tcx: TyCtxt<'_>, def_id: DefId) -> &DefIdSet { - &*tcx.typeck_tables_of(def_id).used_trait_imports + &*if let Some(hir::Node::AnonConst(_)) = tcx.hir().get_if_local(def_id) { + // We don't want to incorrectly suggest constraining type parameters for array lengths. + tcx.const_typeck_tables_of(def_id) + } else { + tcx.typeck_tables_of(def_id) + } + .used_trait_imports } /// Inspects the substs of opaque types, replacing any inference variables @@ -965,7 +990,14 @@ where fn typeck_tables_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &ty::TypeckTables<'tcx> { let fallback = move || tcx.type_of(def_id); - typeck_tables_of_with_fallback(tcx, def_id, fallback) + typeck_tables_of_with_fallback(tcx, def_id, fallback, false) +} + +/// Used only to improve diagnostics when trying to access associated item in a const context. +/// The most common case is trying to use an associated item to set the length of an array. +fn const_typeck_tables_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &ty::TypeckTables<'tcx> { + let fallback = move || tcx.type_of(def_id); + typeck_tables_of_with_fallback(tcx, def_id, fallback, true) } /// Used only to get `TypeckTables` for type inference during error recovery. @@ -980,19 +1012,24 @@ fn diagnostic_only_typeck_tables_of<'tcx>( tcx.sess.delay_span_bug(span, "diagnostic only typeck table used"); tcx.types.err }; - typeck_tables_of_with_fallback(tcx, def_id, fallback) + typeck_tables_of_with_fallback(tcx, def_id, fallback, false) } fn typeck_tables_of_with_fallback<'tcx>( tcx: TyCtxt<'tcx>, def_id: DefId, fallback: impl Fn() -> Ty<'tcx> + 'tcx, + in_const_context: bool, ) -> &'tcx ty::TypeckTables<'tcx> { // Closures' tables come from their outermost function, // as they are part of the same "inference environment". let outer_def_id = tcx.closure_base_def_id(def_id); if outer_def_id != def_id { - return tcx.typeck_tables_of(outer_def_id); + if in_const_context { + return tcx.const_typeck_tables_of(outer_def_id); + } else { + return tcx.typeck_tables_of(outer_def_id); + } } let id = tcx.hir().as_local_hir_id(def_id).unwrap(); @@ -1005,6 +1042,8 @@ fn typeck_tables_of_with_fallback<'tcx>( let body = tcx.hir().body(body_id); let tables = Inherited::build(tcx, def_id).enter(|inh| { + let in_const = inh.in_const_context.get(); + inh.in_const_context.set(in_const_context); let param_env = tcx.param_env(def_id); let fcx = if let (Some(header), Some(decl)) = (fn_header, fn_decl) { let fn_sig = if crate::collect::get_infer_ret_ty(&decl.output).is_some() { @@ -1124,6 +1163,7 @@ fn typeck_tables_of_with_fallback<'tcx>( fcx.regionck_expr(body); } + inh.in_const_context.set(in_const); fcx.resolve_type_vars_in_body(body) }); diff --git a/src/test/ui/associated-const/associated-const-type-parameter-arrays-2.stderr b/src/test/ui/associated-const/associated-const-type-parameter-arrays-2.stderr index ec60db47f4429..61b0a4e0dede3 100644 --- a/src/test/ui/associated-const/associated-const-type-parameter-arrays-2.stderr +++ b/src/test/ui/associated-const/associated-const-type-parameter-arrays-2.stderr @@ -7,11 +7,7 @@ LL | const Y: usize; LL | let _array = [4; ::Y]; | ^^^^^^^^^^^^^ the trait `Foo` is not implemented for `A` | -help: consider further restricting this bound with `+ Foo` - --> $DIR/associated-const-type-parameter-arrays-2.rs:15:16 - | -LL | pub fn test() { - | ^^^ + = note: associated types can't be referenced in `const` contexts, like array in length error: aborting due to previous error diff --git a/src/test/ui/associated-const/associated-const-type-parameter-arrays.stderr b/src/test/ui/associated-const/associated-const-type-parameter-arrays.stderr index 3d38deb5a8763..42e740bd513ba 100644 --- a/src/test/ui/associated-const/associated-const-type-parameter-arrays.stderr +++ b/src/test/ui/associated-const/associated-const-type-parameter-arrays.stderr @@ -7,11 +7,7 @@ LL | const Y: usize; LL | let _array: [u32; ::Y]; | ^^^^^^^^^^^^^ the trait `Foo` is not implemented for `A` | -help: consider further restricting this bound with `+ Foo` - --> $DIR/associated-const-type-parameter-arrays.rs:15:16 - | -LL | pub fn test() { - | ^^^ + = note: associated types can't be referenced in `const` contexts, like array in length error: aborting due to previous error diff --git a/src/test/ui/closures/issue-52437.rs b/src/test/ui/closures/issue-52437.rs index 1e649a556e01d..b58dd57d0a6f4 100644 --- a/src/test/ui/closures/issue-52437.rs +++ b/src/test/ui/closures/issue-52437.rs @@ -3,4 +3,5 @@ fn main() { //~^ ERROR: invalid label name `'static` //~| ERROR: `loop` is not allowed in a `const` //~| ERROR: type annotations needed + //~| ERROR: type annotations needed } diff --git a/src/test/ui/closures/issue-52437.stderr b/src/test/ui/closures/issue-52437.stderr index b9225e55fe5c7..d0a2cea76e4c6 100644 --- a/src/test/ui/closures/issue-52437.stderr +++ b/src/test/ui/closures/issue-52437.stderr @@ -19,7 +19,13 @@ error[E0282]: type annotations needed LL | [(); &(&'static: loop { |x| {}; }) as *const _ as usize] | ^ consider giving this closure parameter a type -error: aborting due to 3 previous errors +error[E0282]: type annotations needed + --> $DIR/issue-52437.rs:2:30 + | +LL | [(); &(&'static: loop { |x| {}; }) as *const _ as usize] + | ^ consider giving this closure parameter a type + +error: aborting due to 4 previous errors Some errors have detailed explanations: E0282, E0658. For more information about an error, try `rustc --explain E0282`. diff --git a/src/test/ui/consts/const-integer-bool-ops.rs b/src/test/ui/consts/const-integer-bool-ops.rs index 6924956bdf706..c8f5b8bfbb5da 100644 --- a/src/test/ui/consts/const-integer-bool-ops.rs +++ b/src/test/ui/consts/const-integer-bool-ops.rs @@ -5,6 +5,9 @@ const X: usize = 42 && 39; //~| expected `bool`, found integer //~| ERROR mismatched types //~| expected `usize`, found `bool` +//~| ERROR mismatched types +//~| ERROR mismatched types +//~| ERROR mismatched types const ARR: [i32; X] = [99; 34]; //~^ ERROR evaluation of constant value failed @@ -15,6 +18,9 @@ const X1: usize = 42 || 39; //~| expected `bool`, found integer //~| ERROR mismatched types //~| expected `usize`, found `bool` +//~| ERROR mismatched types +//~| ERROR mismatched types +//~| ERROR mismatched types const ARR1: [i32; X1] = [99; 47]; //~^ ERROR evaluation of constant value failed @@ -25,52 +31,55 @@ const X2: usize = -42 || -39; //~| expected `bool`, found integer //~| ERROR mismatched types //~| expected `usize`, found `bool` +//~| ERROR mismatched types +//~| ERROR mismatched types +//~| ERROR mismatched types const ARR2: [i32; X2] = [99; 18446744073709551607]; //~^ ERROR evaluation of constant value failed const X3: usize = -42 && -39; //~^ ERROR mismatched types -//~| expected `bool`, found integer //~| ERROR mismatched types -//~| expected `bool`, found integer //~| ERROR mismatched types -//~| expected `usize`, found `bool` +//~| ERROR mismatched types +//~| ERROR mismatched types +//~| ERROR mismatched types const ARR3: [i32; X3] = [99; 6]; //~^ ERROR evaluation of constant value failed const Y: usize = 42.0 == 42.0; //~^ ERROR mismatched types -//~| expected `usize`, found `bool` +//~| ERROR mismatched types const ARRR: [i32; Y] = [99; 1]; //~^ ERROR evaluation of constant value failed const Y1: usize = 42.0 >= 42.0; //~^ ERROR mismatched types -//~| expected `usize`, found `bool` +//~| ERROR mismatched types const ARRR1: [i32; Y1] = [99; 1]; //~^ ERROR evaluation of constant value failed const Y2: usize = 42.0 <= 42.0; //~^ ERROR mismatched types -//~| expected `usize`, found `bool` +//~| ERROR mismatched types const ARRR2: [i32; Y2] = [99; 1]; //~^ ERROR evaluation of constant value failed const Y3: usize = 42.0 > 42.0; //~^ ERROR mismatched types -//~| expected `usize`, found `bool` +//~| ERROR mismatched types const ARRR3: [i32; Y3] = [99; 0]; //~^ ERROR evaluation of constant value failed const Y4: usize = 42.0 < 42.0; //~^ ERROR mismatched types -//~| expected `usize`, found `bool` +//~| ERROR mismatched types const ARRR4: [i32; Y4] = [99; 0]; //~^ ERROR evaluation of constant value failed const Y5: usize = 42.0 != 42.0; //~^ ERROR mismatched types -//~| expected `usize`, found `bool` +//~| ERROR mismatched types const ARRR5: [i32; Y5] = [99; 0]; //~^ ERROR evaluation of constant value failed diff --git a/src/test/ui/consts/const-integer-bool-ops.stderr b/src/test/ui/consts/const-integer-bool-ops.stderr index 9001fefd1029f..0877b48e13bb4 100644 --- a/src/test/ui/consts/const-integer-bool-ops.stderr +++ b/src/test/ui/consts/const-integer-bool-ops.stderr @@ -16,157 +16,265 @@ error[E0308]: mismatched types LL | const X: usize = 42 && 39; | ^^^^^^^^ expected `usize`, found `bool` +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:1:18 + | +LL | const X: usize = 42 && 39; + | ^^ expected `bool`, found integer + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:1:24 + | +LL | const X: usize = 42 && 39; + | ^^ expected `bool`, found integer + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:1:18 + | +LL | const X: usize = 42 && 39; + | ^^^^^^^^ expected `usize`, found `bool` + error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:8:18 + --> $DIR/const-integer-bool-ops.rs:11:18 | LL | const ARR: [i32; X] = [99; 34]; | ^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:11:19 + --> $DIR/const-integer-bool-ops.rs:14:19 | LL | const X1: usize = 42 || 39; | ^^ expected `bool`, found integer error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:11:25 + --> $DIR/const-integer-bool-ops.rs:14:25 | LL | const X1: usize = 42 || 39; | ^^ expected `bool`, found integer error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:11:19 + --> $DIR/const-integer-bool-ops.rs:14:19 + | +LL | const X1: usize = 42 || 39; + | ^^^^^^^^ expected `usize`, found `bool` + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:14:19 + | +LL | const X1: usize = 42 || 39; + | ^^ expected `bool`, found integer + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:14:25 + | +LL | const X1: usize = 42 || 39; + | ^^ expected `bool`, found integer + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:14:19 | LL | const X1: usize = 42 || 39; | ^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:18:19 + --> $DIR/const-integer-bool-ops.rs:24:19 | LL | const ARR1: [i32; X1] = [99; 47]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:21:19 + --> $DIR/const-integer-bool-ops.rs:27:19 + | +LL | const X2: usize = -42 || -39; + | ^^^ expected `bool`, found integer + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:27:26 + | +LL | const X2: usize = -42 || -39; + | ^^^ expected `bool`, found integer + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:27:19 + | +LL | const X2: usize = -42 || -39; + | ^^^^^^^^^^ expected `usize`, found `bool` + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:27:19 | LL | const X2: usize = -42 || -39; | ^^^ expected `bool`, found integer error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:21:26 + --> $DIR/const-integer-bool-ops.rs:27:26 | LL | const X2: usize = -42 || -39; | ^^^ expected `bool`, found integer error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:21:19 + --> $DIR/const-integer-bool-ops.rs:27:19 | LL | const X2: usize = -42 || -39; | ^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:28:19 + --> $DIR/const-integer-bool-ops.rs:37:19 | LL | const ARR2: [i32; X2] = [99; 18446744073709551607]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:31:19 + --> $DIR/const-integer-bool-ops.rs:40:19 | LL | const X3: usize = -42 && -39; | ^^^ expected `bool`, found integer error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:31:26 + --> $DIR/const-integer-bool-ops.rs:40:26 | LL | const X3: usize = -42 && -39; | ^^^ expected `bool`, found integer error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:31:19 + --> $DIR/const-integer-bool-ops.rs:40:19 + | +LL | const X3: usize = -42 && -39; + | ^^^^^^^^^^ expected `usize`, found `bool` + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:40:19 + | +LL | const X3: usize = -42 && -39; + | ^^^ expected `bool`, found integer + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:40:26 + | +LL | const X3: usize = -42 && -39; + | ^^^ expected `bool`, found integer + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:40:19 | LL | const X3: usize = -42 && -39; | ^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:38:19 + --> $DIR/const-integer-bool-ops.rs:47:19 | LL | const ARR3: [i32; X3] = [99; 6]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:41:18 + --> $DIR/const-integer-bool-ops.rs:50:18 + | +LL | const Y: usize = 42.0 == 42.0; + | ^^^^^^^^^^^^ expected `usize`, found `bool` + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:50:18 | LL | const Y: usize = 42.0 == 42.0; | ^^^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:44:19 + --> $DIR/const-integer-bool-ops.rs:53:19 | LL | const ARRR: [i32; Y] = [99; 1]; | ^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:47:19 + --> $DIR/const-integer-bool-ops.rs:56:19 + | +LL | const Y1: usize = 42.0 >= 42.0; + | ^^^^^^^^^^^^ expected `usize`, found `bool` + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:56:19 | LL | const Y1: usize = 42.0 >= 42.0; | ^^^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:50:20 + --> $DIR/const-integer-bool-ops.rs:59:20 | LL | const ARRR1: [i32; Y1] = [99; 1]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:53:19 + --> $DIR/const-integer-bool-ops.rs:62:19 + | +LL | const Y2: usize = 42.0 <= 42.0; + | ^^^^^^^^^^^^ expected `usize`, found `bool` + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:62:19 | LL | const Y2: usize = 42.0 <= 42.0; | ^^^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:56:20 + --> $DIR/const-integer-bool-ops.rs:65:20 | LL | const ARRR2: [i32; Y2] = [99; 1]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:59:19 + --> $DIR/const-integer-bool-ops.rs:68:19 + | +LL | const Y3: usize = 42.0 > 42.0; + | ^^^^^^^^^^^ expected `usize`, found `bool` + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:68:19 | LL | const Y3: usize = 42.0 > 42.0; | ^^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:62:20 + --> $DIR/const-integer-bool-ops.rs:71:20 | LL | const ARRR3: [i32; Y3] = [99; 0]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:65:19 + --> $DIR/const-integer-bool-ops.rs:74:19 + | +LL | const Y4: usize = 42.0 < 42.0; + | ^^^^^^^^^^^ expected `usize`, found `bool` + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:74:19 | LL | const Y4: usize = 42.0 < 42.0; | ^^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:68:20 + --> $DIR/const-integer-bool-ops.rs:77:20 | LL | const ARRR4: [i32; Y4] = [99; 0]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:71:19 + --> $DIR/const-integer-bool-ops.rs:80:19 + | +LL | const Y5: usize = 42.0 != 42.0; + | ^^^^^^^^^^^^ expected `usize`, found `bool` + +error[E0308]: mismatched types + --> $DIR/const-integer-bool-ops.rs:80:19 | LL | const Y5: usize = 42.0 != 42.0; | ^^^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:74:20 + --> $DIR/const-integer-bool-ops.rs:83:20 | LL | const ARRR5: [i32; Y5] = [99; 0]; | ^^ referenced constant has errors -error: aborting due to 28 previous errors +error: aborting due to 46 previous errors Some errors have detailed explanations: E0080, E0308. For more information about an error, try `rustc --explain E0080`. diff --git a/src/test/ui/consts/const-tup-index-span.rs b/src/test/ui/consts/const-tup-index-span.rs index 763263c6aeb4f..c3377a3eca7bf 100644 --- a/src/test/ui/consts/const-tup-index-span.rs +++ b/src/test/ui/consts/const-tup-index-span.rs @@ -2,7 +2,7 @@ const TUP: (usize,) = 5usize << 64; //~^ ERROR mismatched types -//~| expected tuple, found `usize` +//~| ERROR mismatched types const ARR: [i32; TUP.0] = []; //~^ ERROR evaluation of constant value failed diff --git a/src/test/ui/consts/const-tup-index-span.stderr b/src/test/ui/consts/const-tup-index-span.stderr index 8e4a092e40f5e..a283a2e4ed35d 100644 --- a/src/test/ui/consts/const-tup-index-span.stderr +++ b/src/test/ui/consts/const-tup-index-span.stderr @@ -7,13 +7,22 @@ LL | const TUP: (usize,) = 5usize << 64; = note: expected tuple `(usize,)` found type `usize` +error[E0308]: mismatched types + --> $DIR/const-tup-index-span.rs:3:23 + | +LL | const TUP: (usize,) = 5usize << 64; + | ^^^^^^^^^^^^ expected tuple, found `usize` + | + = note: expected tuple `(usize,)` + found type `usize` + error[E0080]: evaluation of constant value failed --> $DIR/const-tup-index-span.rs:6:18 | LL | const ARR: [i32; TUP.0] = []; | ^^^ referenced constant has errors -error: aborting due to 2 previous errors +error: aborting due to 3 previous errors Some errors have detailed explanations: E0080, E0308. For more information about an error, try `rustc --explain E0080`. diff --git a/src/test/ui/consts/issue-52432.rs b/src/test/ui/consts/issue-52432.rs index 2d4c939f47d79..8bd3cac83ec28 100644 --- a/src/test/ui/consts/issue-52432.rs +++ b/src/test/ui/consts/issue-52432.rs @@ -4,6 +4,7 @@ fn main() { [(); &(static |x| {}) as *const _ as usize]; //~^ ERROR: closures cannot be static //~| ERROR: type annotations needed + //~| ERROR: type annotations needed [(); &(static || {}) as *const _ as usize]; //~^ ERROR: closures cannot be static //~| ERROR: evaluation of constant value failed diff --git a/src/test/ui/consts/issue-52432.stderr b/src/test/ui/consts/issue-52432.stderr index e9539d24118a0..6d98c0d4c00e5 100644 --- a/src/test/ui/consts/issue-52432.stderr +++ b/src/test/ui/consts/issue-52432.stderr @@ -5,7 +5,7 @@ LL | [(); &(static |x| {}) as *const _ as usize]; | ^^^^^^^^^^ error[E0697]: closures cannot be static - --> $DIR/issue-52432.rs:7:12 + --> $DIR/issue-52432.rs:8:12 | LL | [(); &(static || {}) as *const _ as usize]; | ^^^^^^^^^ @@ -17,12 +17,18 @@ LL | [(); &(static |x| {}) as *const _ as usize]; | ^ consider giving this closure parameter a type error[E0080]: evaluation of constant value failed - --> $DIR/issue-52432.rs:7:10 + --> $DIR/issue-52432.rs:8:10 | LL | [(); &(static || {}) as *const _ as usize]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants -error: aborting due to 4 previous errors +error[E0282]: type annotations needed + --> $DIR/issue-52432.rs:4:20 + | +LL | [(); &(static |x| {}) as *const _ as usize]; + | ^ consider giving this closure parameter a type + +error: aborting due to 5 previous errors Some errors have detailed explanations: E0080, E0282, E0697. For more information about an error, try `rustc --explain E0080`. diff --git a/src/test/ui/consts/too_generic_eval_ice.stderr b/src/test/ui/consts/too_generic_eval_ice.stderr index 8836de0023c9d..cacf2b316b8a8 100644 --- a/src/test/ui/consts/too_generic_eval_ice.stderr +++ b/src/test/ui/consts/too_generic_eval_ice.stderr @@ -16,30 +16,26 @@ error[E0277]: the size for values of type `A` cannot be known at compilation tim | LL | pub struct Foo(A, B); | --------------------------- required by `Foo` -LL | -LL | impl Foo { - | - this type parameter needs to be `std::marker::Sized` ... LL | [5; Self::HOST_SIZE] == [6; 0] | ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `std::marker::Sized` is not implemented for `A` = note: to learn more, visit + = note: associated types can't be referenced in `const` contexts, like array in length error[E0277]: the size for values of type `B` cannot be known at compilation time --> $DIR/too_generic_eval_ice.rs:7:13 | LL | pub struct Foo(A, B); | --------------------------- required by `Foo` -LL | -LL | impl Foo { - | - this type parameter needs to be `std::marker::Sized` ... LL | [5; Self::HOST_SIZE] == [6; 0] | ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `std::marker::Sized` is not implemented for `B` = note: to learn more, visit + = note: associated types can't be referenced in `const` contexts, like array in length error: aborting due to 3 previous errors diff --git a/src/test/ui/inference/inference_unstable.rs b/src/test/ui/inference/inference_unstable.rs index 0b95770018241..37ebffa68b80f 100644 --- a/src/test/ui/inference/inference_unstable.rs +++ b/src/test/ui/inference/inference_unstable.rs @@ -15,5 +15,7 @@ use inference_unstable_itertools::IpuItertools; fn main() { assert_eq!('x'.ipu_flatten(), 1); //~^ WARN a method with this name may be added to the standard library in the future - //~^^ WARN once this method is added to the standard library, the ambiguity may cause an error + //~| WARN a method with this name may be added to the standard library in the future + //~| WARN once this method is added to the standard library, the ambiguity may cause an error + //~| WARN once this method is added to the standard library, the ambiguity may cause an error } diff --git a/src/test/ui/inference/inference_unstable.stderr b/src/test/ui/inference/inference_unstable.stderr index 1f5cc8b13fb46..18cdb00b22a80 100644 --- a/src/test/ui/inference/inference_unstable.stderr +++ b/src/test/ui/inference/inference_unstable.stderr @@ -10,3 +10,14 @@ LL | assert_eq!('x'.ipu_flatten(), 1); = help: call with fully qualified syntax `inference_unstable_itertools::IpuItertools::ipu_flatten(...)` to keep using the current method = help: add `#![feature(ipu_flatten)]` to the crate attributes to enable `inference_unstable_iterator::IpuIterator::ipu_flatten` +warning: a method with this name may be added to the standard library in the future + --> $DIR/inference_unstable.rs:16:20 + | +LL | assert_eq!('x'.ipu_flatten(), 1); + | ^^^^^^^^^^^ + | + = warning: once this method is added to the standard library, the ambiguity may cause an error or change in behavior! + = note: for more information, see issue #48919 + = help: call with fully qualified syntax `inference_unstable_itertools::IpuItertools::ipu_flatten(...)` to keep using the current method + = help: add `#![feature(ipu_flatten)]` to the crate attributes to enable `inference_unstable_iterator::IpuIterator::ipu_flatten` + diff --git a/src/test/ui/issues/issue-41394.rs b/src/test/ui/issues/issue-41394.rs index 64873ac35a002..72019040798e2 100644 --- a/src/test/ui/issues/issue-41394.rs +++ b/src/test/ui/issues/issue-41394.rs @@ -1,6 +1,7 @@ enum Foo { A = "" + 1 //~^ ERROR cannot add `{integer}` to `&str` + //~| ERROR cannot add `{integer}` to `&str` } enum Bar { diff --git a/src/test/ui/issues/issue-41394.stderr b/src/test/ui/issues/issue-41394.stderr index 47a24547d4533..5216d4bd7ce1e 100644 --- a/src/test/ui/issues/issue-41394.stderr +++ b/src/test/ui/issues/issue-41394.stderr @@ -6,13 +6,21 @@ LL | A = "" + 1 | | | &str +error[E0369]: cannot add `{integer}` to `&str` + --> $DIR/issue-41394.rs:2:12 + | +LL | A = "" + 1 + | -- ^ - {integer} + | | + | &str + error[E0080]: evaluation of constant value failed - --> $DIR/issue-41394.rs:7:9 + --> $DIR/issue-41394.rs:8:9 | LL | A = Foo::A as isize | ^^^^^^^^^^^^^^^ referenced constant has errors -error: aborting due to 2 previous errors +error: aborting due to 3 previous errors Some errors have detailed explanations: E0080, E0369. For more information about an error, try `rustc --explain E0080`. diff --git a/src/test/ui/issues/issue-50688.rs b/src/test/ui/issues/issue-50688.rs index 88f898b86f914..d855dc3ffbf10 100644 --- a/src/test/ui/issues/issue-50688.rs +++ b/src/test/ui/issues/issue-50688.rs @@ -1,3 +1,4 @@ fn main() { [1; || {}]; //~ ERROR mismatched types + //~^ ERROR mismatched types } diff --git a/src/test/ui/issues/issue-50688.stderr b/src/test/ui/issues/issue-50688.stderr index 1f348c4cf1f66..d369835c955dd 100644 --- a/src/test/ui/issues/issue-50688.stderr +++ b/src/test/ui/issues/issue-50688.stderr @@ -7,6 +7,15 @@ LL | [1; || {}]; = note: expected type `usize` found closure `[closure@$DIR/issue-50688.rs:2:9: 2:14]` -error: aborting due to previous error +error[E0308]: mismatched types + --> $DIR/issue-50688.rs:2:9 + | +LL | [1; || {}]; + | ^^^^^ expected `usize`, found closure + | + = note: expected type `usize` + found closure `[closure@$DIR/issue-50688.rs:2:9: 2:14]` + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/issues/issue-51714.rs b/src/test/ui/issues/issue-51714.rs index 782037a1fe5d4..0360a32c66d9b 100644 --- a/src/test/ui/issues/issue-51714.rs +++ b/src/test/ui/issues/issue-51714.rs @@ -1,12 +1,15 @@ fn main() { |_: [_; return || {}] | {}; //~^ ERROR return statement outside of function body + //~| ERROR return statement outside of function body [(); return || {}]; //~^ ERROR return statement outside of function body + //~| ERROR return statement outside of function body [(); return |ice| {}]; //~^ ERROR return statement outside of function body + //~| ERROR return statement outside of function body [(); return while let Some(n) = Some(0) {}]; //~^ ERROR return statement outside of function body diff --git a/src/test/ui/issues/issue-51714.stderr b/src/test/ui/issues/issue-51714.stderr index 696030850436f..2a2a01ed4c656 100644 --- a/src/test/ui/issues/issue-51714.stderr +++ b/src/test/ui/issues/issue-51714.stderr @@ -1,5 +1,5 @@ error[E0658]: `while` is not allowed in a `const` - --> $DIR/issue-51714.rs:11:17 + --> $DIR/issue-51714.rs:14:17 | LL | [(); return while let Some(n) = Some(0) {}]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -15,24 +15,42 @@ LL | |_: [_; return || {}] | {}; | ^^^^^^^^^^^^ error[E0572]: return statement outside of function body - --> $DIR/issue-51714.rs:5:10 + --> $DIR/issue-51714.rs:6:10 | LL | [(); return || {}]; | ^^^^^^^^^^^^ error[E0572]: return statement outside of function body - --> $DIR/issue-51714.rs:8:10 + --> $DIR/issue-51714.rs:10:10 | LL | [(); return |ice| {}]; | ^^^^^^^^^^^^^^^ error[E0572]: return statement outside of function body - --> $DIR/issue-51714.rs:11:10 + --> $DIR/issue-51714.rs:14:10 | LL | [(); return while let Some(n) = Some(0) {}]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 5 previous errors +error[E0572]: return statement outside of function body + --> $DIR/issue-51714.rs:2:14 + | +LL | |_: [_; return || {}] | {}; + | ^^^^^^^^^^^^ + +error[E0572]: return statement outside of function body + --> $DIR/issue-51714.rs:6:10 + | +LL | [(); return || {}]; + | ^^^^^^^^^^^^ + +error[E0572]: return statement outside of function body + --> $DIR/issue-51714.rs:10:10 + | +LL | [(); return |ice| {}]; + | ^^^^^^^^^^^^^^^ + +error: aborting due to 8 previous errors Some errors have detailed explanations: E0572, E0658. For more information about an error, try `rustc --explain E0572`. diff --git a/src/test/ui/issues/issue-54954.rs b/src/test/ui/issues/issue-54954.rs index 3d6355f5c5978..cb889fbf78474 100644 --- a/src/test/ui/issues/issue-54954.rs +++ b/src/test/ui/issues/issue-54954.rs @@ -2,6 +2,7 @@ const ARR_LEN: usize = Tt::const_val::<[i8; 123]>(); //~^ ERROR type annotations needed +//~| ERROR type annotations needed trait Tt { const fn const_val() -> usize { diff --git a/src/test/ui/issues/issue-54954.stderr b/src/test/ui/issues/issue-54954.stderr index 4967b82216e46..e038a95226d2e 100644 --- a/src/test/ui/issues/issue-54954.stderr +++ b/src/test/ui/issues/issue-54954.stderr @@ -1,5 +1,5 @@ error[E0379]: functions in traits cannot be declared const - --> $DIR/issue-54954.rs:7:5 + --> $DIR/issue-54954.rs:8:5 | LL | const fn const_val() -> usize { | ^^^^^ functions in traits cannot be const @@ -15,19 +15,30 @@ LL | const fn const_val() -> usize { | = note: cannot resolve `_: Tt` +error[E0283]: type annotations needed + --> $DIR/issue-54954.rs:3:24 + | +LL | const ARR_LEN: usize = Tt::const_val::<[i8; 123]>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type +... +LL | const fn const_val() -> usize { + | --------- - required by this bound in `Tt::const_val` + | + = note: cannot resolve `_: Tt` + error[E0080]: evaluation of constant value failed - --> $DIR/issue-54954.rs:13:15 + --> $DIR/issue-54954.rs:14:15 | LL | fn f(z: [f32; ARR_LEN]) -> [f32; ARR_LEN] { | ^^^^^^^ referenced constant has errors error[E0080]: evaluation of constant value failed - --> $DIR/issue-54954.rs:13:34 + --> $DIR/issue-54954.rs:14:34 | LL | fn f(z: [f32; ARR_LEN]) -> [f32; ARR_LEN] { | ^^^^^^^ referenced constant has errors -error: aborting due to 4 previous errors +error: aborting due to 5 previous errors Some errors have detailed explanations: E0080, E0283, E0379. For more information about an error, try `rustc --explain E0080`. From 9a821e83eb24f7ad3c0d70d57c160da2bea9fcbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 16 Mar 2020 19:23:26 -0700 Subject: [PATCH 2/5] review comments: avoid changing the queries --- src/librustc/hir/map/mod.rs | 15 +- src/librustc/query/mod.rs | 14 -- src/librustc_infer/infer/mod.rs | 5 - src/librustc_mir/const_eval/eval_queries.rs | 4 +- .../traits/error_reporting/suggestions.rs | 2 +- src/librustc_typeck/check/mod.rs | 52 +----- src/test/ui/closures/issue-52437.rs | 1 - src/test/ui/closures/issue-52437.stderr | 8 +- src/test/ui/consts/const-integer-bool-ops.rs | 27 --- .../ui/consts/const-integer-bool-ops.stderr | 160 +++--------------- src/test/ui/consts/const-tup-index-span.rs | 1 - .../ui/consts/const-tup-index-span.stderr | 13 +- src/test/ui/consts/enum-discr-type-err.stderr | 8 - src/test/ui/consts/issue-52432.rs | 1 - src/test/ui/consts/issue-52432.stderr | 12 +- src/test/ui/discrim/discrim-ill-typed.stderr | 40 ----- src/test/ui/inference/inference_unstable.rs | 2 - .../ui/inference/inference_unstable.stderr | 11 -- src/test/ui/issues/issue-31910.stderr | 5 - src/test/ui/issues/issue-41394.rs | 1 - src/test/ui/issues/issue-41394.stderr | 12 +- src/test/ui/issues/issue-50688.rs | 1 - src/test/ui/issues/issue-50688.stderr | 11 +- src/test/ui/issues/issue-51714.rs | 3 - src/test/ui/issues/issue-51714.stderr | 28 +-- src/test/ui/issues/issue-54954.rs | 1 - src/test/ui/issues/issue-54954.stderr | 19 +-- src/test/ui/issues/issue-8761.stderr | 10 -- src/test/ui/repeat_count.stderr | 10 -- 29 files changed, 61 insertions(+), 416 deletions(-) diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 4c889ef41005c..3efe3d895dca4 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -640,13 +640,13 @@ impl<'hir> Map<'hir> { /// Used exclusively for diagnostics, to avoid suggestion function calls. pub fn is_const_context(&self, hir_id: HirId) -> bool { let parent_id = self.get_parent_item(hir_id); - match self.get(parent_id) { - Node::Item(&Item { kind: ItemKind::Const(..), .. }) - | Node::TraitItem(&TraitItem { kind: TraitItemKind::Const(..), .. }) - | Node::ImplItem(&ImplItem { kind: ImplItemKind::Const(..), .. }) - | Node::AnonConst(_) - | Node::Item(&Item { kind: ItemKind::Static(..), .. }) => true, - Node::Item(&Item { kind: ItemKind::Fn(ref sig, ..), .. }) => { + match self.find(parent_id) { + Some(Node::Item(&Item { kind: ItemKind::Const(..), .. })) + | Some(Node::TraitItem(&TraitItem { kind: TraitItemKind::Const(..), .. })) + | Some(Node::ImplItem(&ImplItem { kind: ImplItemKind::Const(..), .. })) + | Some(Node::AnonConst(_)) + | Some(Node::Item(&Item { kind: ItemKind::Static(..), .. })) => true, + Some(Node::Item(&Item { kind: ItemKind::Fn(ref sig, ..), .. })) => { sig.header.constness == Constness::Const } _ => false, @@ -738,6 +738,7 @@ impl<'hir> Map<'hir> { | Node::Item(_) | Node::ForeignItem(_) | Node::TraitItem(_) + | Node::AnonConst(_) | Node::ImplItem(_) => return hir_id, _ => {} } diff --git a/src/librustc/query/mod.rs b/src/librustc/query/mod.rs index 704f729f77e2c..2dd26397d76f4 100644 --- a/src/librustc/query/mod.rs +++ b/src/librustc/query/mod.rs @@ -438,18 +438,6 @@ rustc_queries! { cache_on_disk_if { key.is_local() } } - query const_typeck_tables_of(key: DefId) -> &'tcx ty::TypeckTables<'tcx> { - desc { |tcx| "type-checking `{}`", tcx.def_path_str(key) } - cache_on_disk_if { key.is_local() } - load_cached(tcx, id) { - let typeck_tables: Option> = tcx - .queries.on_disk_cache - .try_load_query_result(tcx, id); - - typeck_tables.map(|tables| &*tcx.arena.alloc(tables)) - } - } - query diagnostic_only_typeck_tables_of(key: DefId) -> &'tcx ty::TypeckTables<'tcx> { cache_on_disk_if { key.is_local() } load_cached(tcx, id) { @@ -471,8 +459,6 @@ rustc_queries! { TypeChecking { query has_typeck_tables(_: DefId) -> bool {} - query const_has_typeck_tables(_: DefId) -> bool {} - query coherent_trait(def_id: DefId) -> () { desc { |tcx| "coherence checking all impls of trait `{}`", tcx.def_path_str(def_id) } } diff --git a/src/librustc_infer/infer/mod.rs b/src/librustc_infer/infer/mod.rs index e04ae624658dd..9ae131c568d0d 100644 --- a/src/librustc_infer/infer/mod.rs +++ b/src/librustc_infer/infer/mod.rs @@ -250,10 +250,6 @@ pub struct InferCtxt<'a, 'tcx> { /// This flag is true while there is an active snapshot. in_snapshot: Cell, - /// This flag is `true` if the current scope is in a `const` context, - /// like in an array length. Used exclusively to improve diagnostics. - pub in_const_context: Cell, - /// What is the innermost universe we have created? Starts out as /// `UniverseIndex::root()` but grows from there as we enter /// universal quantifiers. @@ -588,7 +584,6 @@ impl<'tcx> InferCtxtBuilder<'tcx> { tainted_by_errors_flag: Cell::new(false), err_count_on_creation: tcx.sess.err_count(), in_snapshot: Cell::new(false), - in_const_context: Cell::new(false), skip_leak_check: Cell::new(false), universe: Cell::new(ty::UniverseIndex::ROOT), }) diff --git a/src/librustc_mir/const_eval/eval_queries.rs b/src/librustc_mir/const_eval/eval_queries.rs index 767caa0a475b0..ffbff00cf3760 100644 --- a/src/librustc_mir/const_eval/eval_queries.rs +++ b/src/librustc_mir/const_eval/eval_queries.rs @@ -290,8 +290,8 @@ pub fn const_eval_raw_provider<'tcx>( let def_id = cid.instance.def.def_id(); if def_id.is_local() - && tcx.const_has_typeck_tables(def_id) - && tcx.const_typeck_tables_of(def_id).tainted_by_errors + && tcx.has_typeck_tables(def_id) + && tcx.typeck_tables_of(def_id).tainted_by_errors { return Err(ErrorHandled::Reported); } diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index f60e6bf81379a..ce9449374ec86 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -158,7 +158,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { ty::Projection(projection) => (false, Some(projection)), _ => return, }; - if self.in_const_context.get() { + if self.tcx.hir().is_const_context(body_id) { err.note( "associated types can't be referenced in `const` contexts, like array in length", ); diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 52040529180e6..1975b24899960 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -753,12 +753,7 @@ fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: DefId) { fn typeck_item_bodies(tcx: TyCtxt<'_>, crate_num: CrateNum) { debug_assert!(crate_num == LOCAL_CRATE); tcx.par_body_owners(|body_owner_def_id| { - if let Some(hir::Node::AnonConst(_)) = tcx.hir().get_if_local(body_owner_def_id) { - // We don't want to incorrectly suggest constraining type parameters for array lengths. - tcx.ensure().const_typeck_tables_of(body_owner_def_id); - } else { - tcx.ensure().typeck_tables_of(body_owner_def_id); - } + tcx.ensure().typeck_tables_of(body_owner_def_id); }); } @@ -779,10 +774,8 @@ pub fn provide(providers: &mut Providers<'_>) { *providers = Providers { typeck_item_bodies, typeck_tables_of, - const_typeck_tables_of, diagnostic_only_typeck_tables_of, has_typeck_tables, - const_has_typeck_tables, adt_destructor, used_trait_imports, check_item_well_formed, @@ -841,23 +834,11 @@ fn primary_body_of( } fn has_typeck_tables(tcx: TyCtxt<'_>, def_id: DefId) -> bool { - has_typeck_tables_inner(tcx, def_id, false) -} - -fn const_has_typeck_tables(tcx: TyCtxt<'_>, def_id: DefId) -> bool { - has_typeck_tables_inner(tcx, def_id, true) -} - -fn has_typeck_tables_inner(tcx: TyCtxt<'_>, def_id: DefId, in_const_context: bool) -> bool { // Closures' tables come from their outermost function, // as they are part of the same "inference environment". let outer_def_id = tcx.closure_base_def_id(def_id); if outer_def_id != def_id { - if in_const_context { - return tcx.const_has_typeck_tables(outer_def_id); - } else { - return tcx.has_typeck_tables(outer_def_id); - } + return tcx.has_typeck_tables(outer_def_id); } if let Some(id) = tcx.hir().as_local_hir_id(def_id) { @@ -868,13 +849,7 @@ fn has_typeck_tables_inner(tcx: TyCtxt<'_>, def_id: DefId, in_const_context: boo } fn used_trait_imports(tcx: TyCtxt<'_>, def_id: DefId) -> &DefIdSet { - &*if let Some(hir::Node::AnonConst(_)) = tcx.hir().get_if_local(def_id) { - // We don't want to incorrectly suggest constraining type parameters for array lengths. - tcx.const_typeck_tables_of(def_id) - } else { - tcx.typeck_tables_of(def_id) - } - .used_trait_imports + &*tcx.typeck_tables_of(def_id).used_trait_imports } /// Inspects the substs of opaque types, replacing any inference variables @@ -990,14 +965,7 @@ where fn typeck_tables_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &ty::TypeckTables<'tcx> { let fallback = move || tcx.type_of(def_id); - typeck_tables_of_with_fallback(tcx, def_id, fallback, false) -} - -/// Used only to improve diagnostics when trying to access associated item in a const context. -/// The most common case is trying to use an associated item to set the length of an array. -fn const_typeck_tables_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &ty::TypeckTables<'tcx> { - let fallback = move || tcx.type_of(def_id); - typeck_tables_of_with_fallback(tcx, def_id, fallback, true) + typeck_tables_of_with_fallback(tcx, def_id, fallback) } /// Used only to get `TypeckTables` for type inference during error recovery. @@ -1012,24 +980,19 @@ fn diagnostic_only_typeck_tables_of<'tcx>( tcx.sess.delay_span_bug(span, "diagnostic only typeck table used"); tcx.types.err }; - typeck_tables_of_with_fallback(tcx, def_id, fallback, false) + typeck_tables_of_with_fallback(tcx, def_id, fallback) } fn typeck_tables_of_with_fallback<'tcx>( tcx: TyCtxt<'tcx>, def_id: DefId, fallback: impl Fn() -> Ty<'tcx> + 'tcx, - in_const_context: bool, ) -> &'tcx ty::TypeckTables<'tcx> { // Closures' tables come from their outermost function, // as they are part of the same "inference environment". let outer_def_id = tcx.closure_base_def_id(def_id); if outer_def_id != def_id { - if in_const_context { - return tcx.const_typeck_tables_of(outer_def_id); - } else { - return tcx.typeck_tables_of(outer_def_id); - } + return tcx.typeck_tables_of(outer_def_id); } let id = tcx.hir().as_local_hir_id(def_id).unwrap(); @@ -1042,8 +1005,6 @@ fn typeck_tables_of_with_fallback<'tcx>( let body = tcx.hir().body(body_id); let tables = Inherited::build(tcx, def_id).enter(|inh| { - let in_const = inh.in_const_context.get(); - inh.in_const_context.set(in_const_context); let param_env = tcx.param_env(def_id); let fcx = if let (Some(header), Some(decl)) = (fn_header, fn_decl) { let fn_sig = if crate::collect::get_infer_ret_ty(&decl.output).is_some() { @@ -1163,7 +1124,6 @@ fn typeck_tables_of_with_fallback<'tcx>( fcx.regionck_expr(body); } - inh.in_const_context.set(in_const); fcx.resolve_type_vars_in_body(body) }); diff --git a/src/test/ui/closures/issue-52437.rs b/src/test/ui/closures/issue-52437.rs index b58dd57d0a6f4..1e649a556e01d 100644 --- a/src/test/ui/closures/issue-52437.rs +++ b/src/test/ui/closures/issue-52437.rs @@ -3,5 +3,4 @@ fn main() { //~^ ERROR: invalid label name `'static` //~| ERROR: `loop` is not allowed in a `const` //~| ERROR: type annotations needed - //~| ERROR: type annotations needed } diff --git a/src/test/ui/closures/issue-52437.stderr b/src/test/ui/closures/issue-52437.stderr index d0a2cea76e4c6..b9225e55fe5c7 100644 --- a/src/test/ui/closures/issue-52437.stderr +++ b/src/test/ui/closures/issue-52437.stderr @@ -19,13 +19,7 @@ error[E0282]: type annotations needed LL | [(); &(&'static: loop { |x| {}; }) as *const _ as usize] | ^ consider giving this closure parameter a type -error[E0282]: type annotations needed - --> $DIR/issue-52437.rs:2:30 - | -LL | [(); &(&'static: loop { |x| {}; }) as *const _ as usize] - | ^ consider giving this closure parameter a type - -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors Some errors have detailed explanations: E0282, E0658. For more information about an error, try `rustc --explain E0282`. diff --git a/src/test/ui/consts/const-integer-bool-ops.rs b/src/test/ui/consts/const-integer-bool-ops.rs index c8f5b8bfbb5da..fc2b2b8b7ff44 100644 --- a/src/test/ui/consts/const-integer-bool-ops.rs +++ b/src/test/ui/consts/const-integer-bool-ops.rs @@ -1,11 +1,5 @@ const X: usize = 42 && 39; //~^ ERROR mismatched types -//~| expected `bool`, found integer -//~| ERROR mismatched types -//~| expected `bool`, found integer -//~| ERROR mismatched types -//~| expected `usize`, found `bool` -//~| ERROR mismatched types //~| ERROR mismatched types //~| ERROR mismatched types const ARR: [i32; X] = [99; 34]; @@ -13,12 +7,6 @@ const ARR: [i32; X] = [99; 34]; const X1: usize = 42 || 39; //~^ ERROR mismatched types -//~| expected `bool`, found integer -//~| ERROR mismatched types -//~| expected `bool`, found integer -//~| ERROR mismatched types -//~| expected `usize`, found `bool` -//~| ERROR mismatched types //~| ERROR mismatched types //~| ERROR mismatched types const ARR1: [i32; X1] = [99; 47]; @@ -26,12 +14,6 @@ const ARR1: [i32; X1] = [99; 47]; const X2: usize = -42 || -39; //~^ ERROR mismatched types -//~| expected `bool`, found integer -//~| ERROR mismatched types -//~| expected `bool`, found integer -//~| ERROR mismatched types -//~| expected `usize`, found `bool` -//~| ERROR mismatched types //~| ERROR mismatched types //~| ERROR mismatched types const ARR2: [i32; X2] = [99; 18446744073709551607]; @@ -41,45 +23,36 @@ const X3: usize = -42 && -39; //~^ ERROR mismatched types //~| ERROR mismatched types //~| ERROR mismatched types -//~| ERROR mismatched types -//~| ERROR mismatched types -//~| ERROR mismatched types const ARR3: [i32; X3] = [99; 6]; //~^ ERROR evaluation of constant value failed const Y: usize = 42.0 == 42.0; //~^ ERROR mismatched types -//~| ERROR mismatched types const ARRR: [i32; Y] = [99; 1]; //~^ ERROR evaluation of constant value failed const Y1: usize = 42.0 >= 42.0; //~^ ERROR mismatched types -//~| ERROR mismatched types const ARRR1: [i32; Y1] = [99; 1]; //~^ ERROR evaluation of constant value failed const Y2: usize = 42.0 <= 42.0; //~^ ERROR mismatched types -//~| ERROR mismatched types const ARRR2: [i32; Y2] = [99; 1]; //~^ ERROR evaluation of constant value failed const Y3: usize = 42.0 > 42.0; //~^ ERROR mismatched types -//~| ERROR mismatched types const ARRR3: [i32; Y3] = [99; 0]; //~^ ERROR evaluation of constant value failed const Y4: usize = 42.0 < 42.0; //~^ ERROR mismatched types -//~| ERROR mismatched types const ARRR4: [i32; Y4] = [99; 0]; //~^ ERROR evaluation of constant value failed const Y5: usize = 42.0 != 42.0; //~^ ERROR mismatched types -//~| ERROR mismatched types const ARRR5: [i32; Y5] = [99; 0]; //~^ ERROR evaluation of constant value failed diff --git a/src/test/ui/consts/const-integer-bool-ops.stderr b/src/test/ui/consts/const-integer-bool-ops.stderr index 0877b48e13bb4..ca0908722712f 100644 --- a/src/test/ui/consts/const-integer-bool-ops.stderr +++ b/src/test/ui/consts/const-integer-bool-ops.stderr @@ -16,265 +16,157 @@ error[E0308]: mismatched types LL | const X: usize = 42 && 39; | ^^^^^^^^ expected `usize`, found `bool` -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:1:18 - | -LL | const X: usize = 42 && 39; - | ^^ expected `bool`, found integer - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:1:24 - | -LL | const X: usize = 42 && 39; - | ^^ expected `bool`, found integer - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:1:18 - | -LL | const X: usize = 42 && 39; - | ^^^^^^^^ expected `usize`, found `bool` - error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:11:18 + --> $DIR/const-integer-bool-ops.rs:5:18 | LL | const ARR: [i32; X] = [99; 34]; | ^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:14:19 + --> $DIR/const-integer-bool-ops.rs:8:19 | LL | const X1: usize = 42 || 39; | ^^ expected `bool`, found integer error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:14:25 + --> $DIR/const-integer-bool-ops.rs:8:25 | LL | const X1: usize = 42 || 39; | ^^ expected `bool`, found integer error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:14:19 - | -LL | const X1: usize = 42 || 39; - | ^^^^^^^^ expected `usize`, found `bool` - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:14:19 - | -LL | const X1: usize = 42 || 39; - | ^^ expected `bool`, found integer - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:14:25 - | -LL | const X1: usize = 42 || 39; - | ^^ expected `bool`, found integer - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:14:19 + --> $DIR/const-integer-bool-ops.rs:8:19 | LL | const X1: usize = 42 || 39; | ^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:24:19 + --> $DIR/const-integer-bool-ops.rs:12:19 | LL | const ARR1: [i32; X1] = [99; 47]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:27:19 + --> $DIR/const-integer-bool-ops.rs:15:19 | LL | const X2: usize = -42 || -39; | ^^^ expected `bool`, found integer error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:27:26 + --> $DIR/const-integer-bool-ops.rs:15:26 | LL | const X2: usize = -42 || -39; | ^^^ expected `bool`, found integer error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:27:19 - | -LL | const X2: usize = -42 || -39; - | ^^^^^^^^^^ expected `usize`, found `bool` - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:27:19 - | -LL | const X2: usize = -42 || -39; - | ^^^ expected `bool`, found integer - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:27:26 - | -LL | const X2: usize = -42 || -39; - | ^^^ expected `bool`, found integer - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:27:19 + --> $DIR/const-integer-bool-ops.rs:15:19 | LL | const X2: usize = -42 || -39; | ^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:37:19 + --> $DIR/const-integer-bool-ops.rs:19:19 | LL | const ARR2: [i32; X2] = [99; 18446744073709551607]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:40:19 + --> $DIR/const-integer-bool-ops.rs:22:19 | LL | const X3: usize = -42 && -39; | ^^^ expected `bool`, found integer error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:40:26 + --> $DIR/const-integer-bool-ops.rs:22:26 | LL | const X3: usize = -42 && -39; | ^^^ expected `bool`, found integer error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:40:19 - | -LL | const X3: usize = -42 && -39; - | ^^^^^^^^^^ expected `usize`, found `bool` - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:40:19 - | -LL | const X3: usize = -42 && -39; - | ^^^ expected `bool`, found integer - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:40:26 - | -LL | const X3: usize = -42 && -39; - | ^^^ expected `bool`, found integer - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:40:19 + --> $DIR/const-integer-bool-ops.rs:22:19 | LL | const X3: usize = -42 && -39; | ^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:47:19 + --> $DIR/const-integer-bool-ops.rs:26:19 | LL | const ARR3: [i32; X3] = [99; 6]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:50:18 - | -LL | const Y: usize = 42.0 == 42.0; - | ^^^^^^^^^^^^ expected `usize`, found `bool` - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:50:18 + --> $DIR/const-integer-bool-ops.rs:29:18 | LL | const Y: usize = 42.0 == 42.0; | ^^^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:53:19 + --> $DIR/const-integer-bool-ops.rs:31:19 | LL | const ARRR: [i32; Y] = [99; 1]; | ^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:56:19 - | -LL | const Y1: usize = 42.0 >= 42.0; - | ^^^^^^^^^^^^ expected `usize`, found `bool` - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:56:19 + --> $DIR/const-integer-bool-ops.rs:34:19 | LL | const Y1: usize = 42.0 >= 42.0; | ^^^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:59:20 + --> $DIR/const-integer-bool-ops.rs:36:20 | LL | const ARRR1: [i32; Y1] = [99; 1]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:62:19 - | -LL | const Y2: usize = 42.0 <= 42.0; - | ^^^^^^^^^^^^ expected `usize`, found `bool` - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:62:19 + --> $DIR/const-integer-bool-ops.rs:39:19 | LL | const Y2: usize = 42.0 <= 42.0; | ^^^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:65:20 + --> $DIR/const-integer-bool-ops.rs:41:20 | LL | const ARRR2: [i32; Y2] = [99; 1]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:68:19 - | -LL | const Y3: usize = 42.0 > 42.0; - | ^^^^^^^^^^^ expected `usize`, found `bool` - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:68:19 + --> $DIR/const-integer-bool-ops.rs:44:19 | LL | const Y3: usize = 42.0 > 42.0; | ^^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:71:20 + --> $DIR/const-integer-bool-ops.rs:46:20 | LL | const ARRR3: [i32; Y3] = [99; 0]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:74:19 - | -LL | const Y4: usize = 42.0 < 42.0; - | ^^^^^^^^^^^ expected `usize`, found `bool` - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:74:19 + --> $DIR/const-integer-bool-ops.rs:49:19 | LL | const Y4: usize = 42.0 < 42.0; | ^^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:77:20 + --> $DIR/const-integer-bool-ops.rs:51:20 | LL | const ARRR4: [i32; Y4] = [99; 0]; | ^^ referenced constant has errors error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:80:19 - | -LL | const Y5: usize = 42.0 != 42.0; - | ^^^^^^^^^^^^ expected `usize`, found `bool` - -error[E0308]: mismatched types - --> $DIR/const-integer-bool-ops.rs:80:19 + --> $DIR/const-integer-bool-ops.rs:54:19 | LL | const Y5: usize = 42.0 != 42.0; | ^^^^^^^^^^^^ expected `usize`, found `bool` error[E0080]: evaluation of constant value failed - --> $DIR/const-integer-bool-ops.rs:83:20 + --> $DIR/const-integer-bool-ops.rs:56:20 | LL | const ARRR5: [i32; Y5] = [99; 0]; | ^^ referenced constant has errors -error: aborting due to 46 previous errors +error: aborting due to 28 previous errors Some errors have detailed explanations: E0080, E0308. For more information about an error, try `rustc --explain E0080`. diff --git a/src/test/ui/consts/const-tup-index-span.rs b/src/test/ui/consts/const-tup-index-span.rs index c3377a3eca7bf..ed8779ab1c46d 100644 --- a/src/test/ui/consts/const-tup-index-span.rs +++ b/src/test/ui/consts/const-tup-index-span.rs @@ -2,7 +2,6 @@ const TUP: (usize,) = 5usize << 64; //~^ ERROR mismatched types -//~| ERROR mismatched types const ARR: [i32; TUP.0] = []; //~^ ERROR evaluation of constant value failed diff --git a/src/test/ui/consts/const-tup-index-span.stderr b/src/test/ui/consts/const-tup-index-span.stderr index a283a2e4ed35d..aaf4e9eea4bbb 100644 --- a/src/test/ui/consts/const-tup-index-span.stderr +++ b/src/test/ui/consts/const-tup-index-span.stderr @@ -7,22 +7,13 @@ LL | const TUP: (usize,) = 5usize << 64; = note: expected tuple `(usize,)` found type `usize` -error[E0308]: mismatched types - --> $DIR/const-tup-index-span.rs:3:23 - | -LL | const TUP: (usize,) = 5usize << 64; - | ^^^^^^^^^^^^ expected tuple, found `usize` - | - = note: expected tuple `(usize,)` - found type `usize` - error[E0080]: evaluation of constant value failed - --> $DIR/const-tup-index-span.rs:6:18 + --> $DIR/const-tup-index-span.rs:5:18 | LL | const ARR: [i32; TUP.0] = []; | ^^^ referenced constant has errors -error: aborting due to 3 previous errors +error: aborting due to 2 previous errors Some errors have detailed explanations: E0080, E0308. For more information about an error, try `rustc --explain E0080`. diff --git a/src/test/ui/consts/enum-discr-type-err.stderr b/src/test/ui/consts/enum-discr-type-err.stderr index 492b79e2e6021..9834a99b79a0e 100644 --- a/src/test/ui/consts/enum-discr-type-err.stderr +++ b/src/test/ui/consts/enum-discr-type-err.stderr @@ -11,10 +11,6 @@ LL | | } | |_- in this macro invocation | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -help: you can convert an `i32` to `isize` and panic if the converted value wouldn't fit - | -LL | $( $v = $s::V.try_into().unwrap(), )* - | ^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0308]: mismatched types --> $DIR/enum-discr-type-err.rs:18:21 @@ -29,10 +25,6 @@ LL | | } | |_- in this macro invocation | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -help: you can convert an `i32` to `isize` and panic if the converted value wouldn't fit - | -LL | $( $v = $s::V.try_into().unwrap(), )* - | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 2 previous errors diff --git a/src/test/ui/consts/issue-52432.rs b/src/test/ui/consts/issue-52432.rs index 8bd3cac83ec28..2d4c939f47d79 100644 --- a/src/test/ui/consts/issue-52432.rs +++ b/src/test/ui/consts/issue-52432.rs @@ -4,7 +4,6 @@ fn main() { [(); &(static |x| {}) as *const _ as usize]; //~^ ERROR: closures cannot be static //~| ERROR: type annotations needed - //~| ERROR: type annotations needed [(); &(static || {}) as *const _ as usize]; //~^ ERROR: closures cannot be static //~| ERROR: evaluation of constant value failed diff --git a/src/test/ui/consts/issue-52432.stderr b/src/test/ui/consts/issue-52432.stderr index 6d98c0d4c00e5..e9539d24118a0 100644 --- a/src/test/ui/consts/issue-52432.stderr +++ b/src/test/ui/consts/issue-52432.stderr @@ -5,7 +5,7 @@ LL | [(); &(static |x| {}) as *const _ as usize]; | ^^^^^^^^^^ error[E0697]: closures cannot be static - --> $DIR/issue-52432.rs:8:12 + --> $DIR/issue-52432.rs:7:12 | LL | [(); &(static || {}) as *const _ as usize]; | ^^^^^^^^^ @@ -17,18 +17,12 @@ LL | [(); &(static |x| {}) as *const _ as usize]; | ^ consider giving this closure parameter a type error[E0080]: evaluation of constant value failed - --> $DIR/issue-52432.rs:8:10 + --> $DIR/issue-52432.rs:7:10 | LL | [(); &(static || {}) as *const _ as usize]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants -error[E0282]: type annotations needed - --> $DIR/issue-52432.rs:4:20 - | -LL | [(); &(static |x| {}) as *const _ as usize]; - | ^ consider giving this closure parameter a type - -error: aborting due to 5 previous errors +error: aborting due to 4 previous errors Some errors have detailed explanations: E0080, E0282, E0697. For more information about an error, try `rustc --explain E0080`. diff --git a/src/test/ui/discrim/discrim-ill-typed.stderr b/src/test/ui/discrim/discrim-ill-typed.stderr index 7b9f086151a84..b0e11ee5a6e60 100644 --- a/src/test/ui/discrim/discrim-ill-typed.stderr +++ b/src/test/ui/discrim/discrim-ill-typed.stderr @@ -3,88 +3,48 @@ error[E0308]: mismatched types | LL | OhNo = 0_u8, | ^^^^ expected `i8`, found `u8` - | -help: change the type of the numeric literal from `u8` to `i8` - | -LL | OhNo = 0_i8, - | ^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:30:16 | LL | OhNo = 0_i8, | ^^^^ expected `u8`, found `i8` - | -help: change the type of the numeric literal from `i8` to `u8` - | -LL | OhNo = 0_u8, - | ^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:43:16 | LL | OhNo = 0_u16, | ^^^^^ expected `i16`, found `u16` - | -help: change the type of the numeric literal from `u16` to `i16` - | -LL | OhNo = 0_i16, - | ^^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:56:16 | LL | OhNo = 0_i16, | ^^^^^ expected `u16`, found `i16` - | -help: change the type of the numeric literal from `i16` to `u16` - | -LL | OhNo = 0_u16, - | ^^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:69:16 | LL | OhNo = 0_u32, | ^^^^^ expected `i32`, found `u32` - | -help: change the type of the numeric literal from `u32` to `i32` - | -LL | OhNo = 0_i32, - | ^^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:82:16 | LL | OhNo = 0_i32, | ^^^^^ expected `u32`, found `i32` - | -help: change the type of the numeric literal from `i32` to `u32` - | -LL | OhNo = 0_u32, - | ^^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:95:16 | LL | OhNo = 0_u64, | ^^^^^ expected `i64`, found `u64` - | -help: change the type of the numeric literal from `u64` to `i64` - | -LL | OhNo = 0_i64, - | ^^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:108:16 | LL | OhNo = 0_i64, | ^^^^^ expected `u64`, found `i64` - | -help: change the type of the numeric literal from `i64` to `u64` - | -LL | OhNo = 0_u64, - | ^^^^^ error: aborting due to 8 previous errors diff --git a/src/test/ui/inference/inference_unstable.rs b/src/test/ui/inference/inference_unstable.rs index 37ebffa68b80f..1d31c0156c3a9 100644 --- a/src/test/ui/inference/inference_unstable.rs +++ b/src/test/ui/inference/inference_unstable.rs @@ -15,7 +15,5 @@ use inference_unstable_itertools::IpuItertools; fn main() { assert_eq!('x'.ipu_flatten(), 1); //~^ WARN a method with this name may be added to the standard library in the future - //~| WARN a method with this name may be added to the standard library in the future - //~| WARN once this method is added to the standard library, the ambiguity may cause an error //~| WARN once this method is added to the standard library, the ambiguity may cause an error } diff --git a/src/test/ui/inference/inference_unstable.stderr b/src/test/ui/inference/inference_unstable.stderr index 18cdb00b22a80..1f5cc8b13fb46 100644 --- a/src/test/ui/inference/inference_unstable.stderr +++ b/src/test/ui/inference/inference_unstable.stderr @@ -10,14 +10,3 @@ LL | assert_eq!('x'.ipu_flatten(), 1); = help: call with fully qualified syntax `inference_unstable_itertools::IpuItertools::ipu_flatten(...)` to keep using the current method = help: add `#![feature(ipu_flatten)]` to the crate attributes to enable `inference_unstable_iterator::IpuIterator::ipu_flatten` -warning: a method with this name may be added to the standard library in the future - --> $DIR/inference_unstable.rs:16:20 - | -LL | assert_eq!('x'.ipu_flatten(), 1); - | ^^^^^^^^^^^ - | - = warning: once this method is added to the standard library, the ambiguity may cause an error or change in behavior! - = note: for more information, see issue #48919 - = help: call with fully qualified syntax `inference_unstable_itertools::IpuItertools::ipu_flatten(...)` to keep using the current method - = help: add `#![feature(ipu_flatten)]` to the crate attributes to enable `inference_unstable_iterator::IpuIterator::ipu_flatten` - diff --git a/src/test/ui/issues/issue-31910.stderr b/src/test/ui/issues/issue-31910.stderr index c5c988cdaa75a..2603c944207d0 100644 --- a/src/test/ui/issues/issue-31910.stderr +++ b/src/test/ui/issues/issue-31910.stderr @@ -3,11 +3,6 @@ error[E0308]: mismatched types | LL | X = Trait::Number, | ^^^^^^^^^^^^^ expected `isize`, found `i32` - | -help: you can convert an `i32` to `isize` and panic if the converted value wouldn't fit - | -LL | X = Trait::Number.try_into().unwrap(), - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to previous error diff --git a/src/test/ui/issues/issue-41394.rs b/src/test/ui/issues/issue-41394.rs index 72019040798e2..64873ac35a002 100644 --- a/src/test/ui/issues/issue-41394.rs +++ b/src/test/ui/issues/issue-41394.rs @@ -1,7 +1,6 @@ enum Foo { A = "" + 1 //~^ ERROR cannot add `{integer}` to `&str` - //~| ERROR cannot add `{integer}` to `&str` } enum Bar { diff --git a/src/test/ui/issues/issue-41394.stderr b/src/test/ui/issues/issue-41394.stderr index 5216d4bd7ce1e..47a24547d4533 100644 --- a/src/test/ui/issues/issue-41394.stderr +++ b/src/test/ui/issues/issue-41394.stderr @@ -6,21 +6,13 @@ LL | A = "" + 1 | | | &str -error[E0369]: cannot add `{integer}` to `&str` - --> $DIR/issue-41394.rs:2:12 - | -LL | A = "" + 1 - | -- ^ - {integer} - | | - | &str - error[E0080]: evaluation of constant value failed - --> $DIR/issue-41394.rs:8:9 + --> $DIR/issue-41394.rs:7:9 | LL | A = Foo::A as isize | ^^^^^^^^^^^^^^^ referenced constant has errors -error: aborting due to 3 previous errors +error: aborting due to 2 previous errors Some errors have detailed explanations: E0080, E0369. For more information about an error, try `rustc --explain E0080`. diff --git a/src/test/ui/issues/issue-50688.rs b/src/test/ui/issues/issue-50688.rs index d855dc3ffbf10..88f898b86f914 100644 --- a/src/test/ui/issues/issue-50688.rs +++ b/src/test/ui/issues/issue-50688.rs @@ -1,4 +1,3 @@ fn main() { [1; || {}]; //~ ERROR mismatched types - //~^ ERROR mismatched types } diff --git a/src/test/ui/issues/issue-50688.stderr b/src/test/ui/issues/issue-50688.stderr index d369835c955dd..1f348c4cf1f66 100644 --- a/src/test/ui/issues/issue-50688.stderr +++ b/src/test/ui/issues/issue-50688.stderr @@ -7,15 +7,6 @@ LL | [1; || {}]; = note: expected type `usize` found closure `[closure@$DIR/issue-50688.rs:2:9: 2:14]` -error[E0308]: mismatched types - --> $DIR/issue-50688.rs:2:9 - | -LL | [1; || {}]; - | ^^^^^ expected `usize`, found closure - | - = note: expected type `usize` - found closure `[closure@$DIR/issue-50688.rs:2:9: 2:14]` - -error: aborting due to 2 previous errors +error: aborting due to previous error For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/issues/issue-51714.rs b/src/test/ui/issues/issue-51714.rs index 0360a32c66d9b..782037a1fe5d4 100644 --- a/src/test/ui/issues/issue-51714.rs +++ b/src/test/ui/issues/issue-51714.rs @@ -1,15 +1,12 @@ fn main() { |_: [_; return || {}] | {}; //~^ ERROR return statement outside of function body - //~| ERROR return statement outside of function body [(); return || {}]; //~^ ERROR return statement outside of function body - //~| ERROR return statement outside of function body [(); return |ice| {}]; //~^ ERROR return statement outside of function body - //~| ERROR return statement outside of function body [(); return while let Some(n) = Some(0) {}]; //~^ ERROR return statement outside of function body diff --git a/src/test/ui/issues/issue-51714.stderr b/src/test/ui/issues/issue-51714.stderr index 2a2a01ed4c656..696030850436f 100644 --- a/src/test/ui/issues/issue-51714.stderr +++ b/src/test/ui/issues/issue-51714.stderr @@ -1,5 +1,5 @@ error[E0658]: `while` is not allowed in a `const` - --> $DIR/issue-51714.rs:14:17 + --> $DIR/issue-51714.rs:11:17 | LL | [(); return while let Some(n) = Some(0) {}]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -15,42 +15,24 @@ LL | |_: [_; return || {}] | {}; | ^^^^^^^^^^^^ error[E0572]: return statement outside of function body - --> $DIR/issue-51714.rs:6:10 + --> $DIR/issue-51714.rs:5:10 | LL | [(); return || {}]; | ^^^^^^^^^^^^ error[E0572]: return statement outside of function body - --> $DIR/issue-51714.rs:10:10 + --> $DIR/issue-51714.rs:8:10 | LL | [(); return |ice| {}]; | ^^^^^^^^^^^^^^^ error[E0572]: return statement outside of function body - --> $DIR/issue-51714.rs:14:10 + --> $DIR/issue-51714.rs:11:10 | LL | [(); return while let Some(n) = Some(0) {}]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0572]: return statement outside of function body - --> $DIR/issue-51714.rs:2:14 - | -LL | |_: [_; return || {}] | {}; - | ^^^^^^^^^^^^ - -error[E0572]: return statement outside of function body - --> $DIR/issue-51714.rs:6:10 - | -LL | [(); return || {}]; - | ^^^^^^^^^^^^ - -error[E0572]: return statement outside of function body - --> $DIR/issue-51714.rs:10:10 - | -LL | [(); return |ice| {}]; - | ^^^^^^^^^^^^^^^ - -error: aborting due to 8 previous errors +error: aborting due to 5 previous errors Some errors have detailed explanations: E0572, E0658. For more information about an error, try `rustc --explain E0572`. diff --git a/src/test/ui/issues/issue-54954.rs b/src/test/ui/issues/issue-54954.rs index cb889fbf78474..3d6355f5c5978 100644 --- a/src/test/ui/issues/issue-54954.rs +++ b/src/test/ui/issues/issue-54954.rs @@ -2,7 +2,6 @@ const ARR_LEN: usize = Tt::const_val::<[i8; 123]>(); //~^ ERROR type annotations needed -//~| ERROR type annotations needed trait Tt { const fn const_val() -> usize { diff --git a/src/test/ui/issues/issue-54954.stderr b/src/test/ui/issues/issue-54954.stderr index e038a95226d2e..4967b82216e46 100644 --- a/src/test/ui/issues/issue-54954.stderr +++ b/src/test/ui/issues/issue-54954.stderr @@ -1,5 +1,5 @@ error[E0379]: functions in traits cannot be declared const - --> $DIR/issue-54954.rs:8:5 + --> $DIR/issue-54954.rs:7:5 | LL | const fn const_val() -> usize { | ^^^^^ functions in traits cannot be const @@ -15,30 +15,19 @@ LL | const fn const_val() -> usize { | = note: cannot resolve `_: Tt` -error[E0283]: type annotations needed - --> $DIR/issue-54954.rs:3:24 - | -LL | const ARR_LEN: usize = Tt::const_val::<[i8; 123]>(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type -... -LL | const fn const_val() -> usize { - | --------- - required by this bound in `Tt::const_val` - | - = note: cannot resolve `_: Tt` - error[E0080]: evaluation of constant value failed - --> $DIR/issue-54954.rs:14:15 + --> $DIR/issue-54954.rs:13:15 | LL | fn f(z: [f32; ARR_LEN]) -> [f32; ARR_LEN] { | ^^^^^^^ referenced constant has errors error[E0080]: evaluation of constant value failed - --> $DIR/issue-54954.rs:14:34 + --> $DIR/issue-54954.rs:13:34 | LL | fn f(z: [f32; ARR_LEN]) -> [f32; ARR_LEN] { | ^^^^^^^ referenced constant has errors -error: aborting due to 5 previous errors +error: aborting due to 4 previous errors Some errors have detailed explanations: E0080, E0283, E0379. For more information about an error, try `rustc --explain E0080`. diff --git a/src/test/ui/issues/issue-8761.stderr b/src/test/ui/issues/issue-8761.stderr index 836520a28ef34..6ab74a9e98940 100644 --- a/src/test/ui/issues/issue-8761.stderr +++ b/src/test/ui/issues/issue-8761.stderr @@ -3,22 +3,12 @@ error[E0308]: mismatched types | LL | A = 1i64, | ^^^^ expected `isize`, found `i64` - | -help: change the type of the numeric literal from `i64` to `isize` - | -LL | A = 1isize, - | ^^^^^^ error[E0308]: mismatched types --> $DIR/issue-8761.rs:5:9 | LL | B = 2u8 | ^^^ expected `isize`, found `u8` - | -help: change the type of the numeric literal from `u8` to `isize` - | -LL | B = 2isize - | ^^^^^^ error: aborting due to 2 previous errors diff --git a/src/test/ui/repeat_count.stderr b/src/test/ui/repeat_count.stderr index efad00b272cdc..df100a09b5635 100644 --- a/src/test/ui/repeat_count.stderr +++ b/src/test/ui/repeat_count.stderr @@ -33,22 +33,12 @@ error[E0308]: mismatched types | LL | let f = [0; -4_isize]; | ^^^^^^^^ expected `usize`, found `isize` - | -help: you can convert an `isize` to `usize` and panic if the converted value wouldn't fit - | -LL | let f = [0; (-4_isize).try_into().unwrap()]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0308]: mismatched types --> $DIR/repeat_count.rs:22:23 | LL | let f = [0_usize; -1_isize]; | ^^^^^^^^ expected `usize`, found `isize` - | -help: you can convert an `isize` to `usize` and panic if the converted value wouldn't fit - | -LL | let f = [0_usize; (-1_isize).try_into().unwrap()]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0308]: mismatched types --> $DIR/repeat_count.rs:28:17 From f00ead191ce8a08cd204cb7c9c69134a1533373d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 16 Mar 2020 19:34:45 -0700 Subject: [PATCH 3/5] review comment: fix wording --- .../traits/error_reporting/suggestions.rs | 3 ++- .../associated-const-type-parameter-arrays-2.stderr | 2 +- .../associated-const-type-parameter-arrays.stderr | 2 +- src/test/ui/consts/too_generic_eval_ice.stderr | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index ce9449374ec86..2d83c7311c122 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -160,7 +160,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { }; if self.tcx.hir().is_const_context(body_id) { err.note( - "associated types can't be referenced in `const` contexts, like array in length", + "associated types cannot be referenced in `const` contexts, \ + like the length of an array", ); return; } diff --git a/src/test/ui/associated-const/associated-const-type-parameter-arrays-2.stderr b/src/test/ui/associated-const/associated-const-type-parameter-arrays-2.stderr index 61b0a4e0dede3..310d3adc3a930 100644 --- a/src/test/ui/associated-const/associated-const-type-parameter-arrays-2.stderr +++ b/src/test/ui/associated-const/associated-const-type-parameter-arrays-2.stderr @@ -7,7 +7,7 @@ LL | const Y: usize; LL | let _array = [4; ::Y]; | ^^^^^^^^^^^^^ the trait `Foo` is not implemented for `A` | - = note: associated types can't be referenced in `const` contexts, like array in length + = note: associated types cannot be referenced in `const` contexts, like the length of an array error: aborting due to previous error diff --git a/src/test/ui/associated-const/associated-const-type-parameter-arrays.stderr b/src/test/ui/associated-const/associated-const-type-parameter-arrays.stderr index 42e740bd513ba..5ded42efd498a 100644 --- a/src/test/ui/associated-const/associated-const-type-parameter-arrays.stderr +++ b/src/test/ui/associated-const/associated-const-type-parameter-arrays.stderr @@ -7,7 +7,7 @@ LL | const Y: usize; LL | let _array: [u32; ::Y]; | ^^^^^^^^^^^^^ the trait `Foo` is not implemented for `A` | - = note: associated types can't be referenced in `const` contexts, like array in length + = note: associated types cannot be referenced in `const` contexts, like the length of an array error: aborting due to previous error diff --git a/src/test/ui/consts/too_generic_eval_ice.stderr b/src/test/ui/consts/too_generic_eval_ice.stderr index cacf2b316b8a8..0f8bbea941d58 100644 --- a/src/test/ui/consts/too_generic_eval_ice.stderr +++ b/src/test/ui/consts/too_generic_eval_ice.stderr @@ -22,7 +22,7 @@ LL | [5; Self::HOST_SIZE] == [6; 0] | = help: the trait `std::marker::Sized` is not implemented for `A` = note: to learn more, visit - = note: associated types can't be referenced in `const` contexts, like array in length + = note: associated types cannot be referenced in `const` contexts, like the length of an array error[E0277]: the size for values of type `B` cannot be known at compilation time --> $DIR/too_generic_eval_ice.rs:7:13 @@ -35,7 +35,7 @@ LL | [5; Self::HOST_SIZE] == [6; 0] | = help: the trait `std::marker::Sized` is not implemented for `B` = note: to learn more, visit - = note: associated types can't be referenced in `const` contexts, like array in length + = note: associated types cannot be referenced in `const` contexts, like the length of an array error: aborting due to 3 previous errors From 84a5706abec65cd0763cd00eb80044006aedf6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 17 Mar 2020 09:53:50 -0700 Subject: [PATCH 4/5] Rework `is_const_context` to not rely on `get_parent_item` --- src/librustc/hir/map/mod.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 3efe3d895dca4..86b5ef73d4446 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -639,18 +639,22 @@ impl<'hir> Map<'hir> { /// Whether the expression pointed at by `hir_id` belongs to a `const` evaluation context. /// Used exclusively for diagnostics, to avoid suggestion function calls. pub fn is_const_context(&self, hir_id: HirId) -> bool { - let parent_id = self.get_parent_item(hir_id); - match self.find(parent_id) { - Some(Node::Item(&Item { kind: ItemKind::Const(..), .. })) - | Some(Node::TraitItem(&TraitItem { kind: TraitItemKind::Const(..), .. })) - | Some(Node::ImplItem(&ImplItem { kind: ImplItemKind::Const(..), .. })) - | Some(Node::AnonConst(_)) - | Some(Node::Item(&Item { kind: ItemKind::Static(..), .. })) => true, - Some(Node::Item(&Item { kind: ItemKind::Fn(ref sig, ..), .. })) => { - sig.header.constness == Constness::Const + for (_, node) in self.parent_iter(hir_id) { + match node { + Node::Item(&Item { kind: ItemKind::Const(..), .. }) + | Node::TraitItem(&TraitItem { kind: TraitItemKind::Const(..), .. }) + | Node::ImplItem(&ImplItem { kind: ImplItemKind::Const(..), .. }) + | Node::AnonConst(_) + | Node::Item(&Item { kind: ItemKind::Static(..), .. }) => return true, + Node::Item(&Item { kind: ItemKind::Fn(ref sig, ..), .. }) => { + if sig.header.constness == Constness::Const { + return true; + } + } + _ => {} } - _ => false, } + false } /// Whether `hir_id` corresponds to a `mod` or a crate. @@ -738,7 +742,6 @@ impl<'hir> Map<'hir> { | Node::Item(_) | Node::ForeignItem(_) | Node::TraitItem(_) - | Node::AnonConst(_) | Node::ImplItem(_) => return hir_id, _ => {} } From f02aeae21080c9fe1dd09a51a0594f83716f565b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 17 Mar 2020 18:57:34 -0700 Subject: [PATCH 5/5] Fix `check_for_cast` --- src/librustc_typeck/check/demand.rs | 46 ++++++++++---------- src/test/ui/discrim/discrim-ill-typed.stderr | 40 +++++++++++++++++ src/test/ui/issues/issue-8761.stderr | 10 +++++ src/test/ui/numeric/const-scope.stderr | 10 +++++ 4 files changed, 83 insertions(+), 23 deletions(-) diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 0556c80e4f707..6c60e5a9c429f 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -598,11 +598,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { checked_ty: Ty<'tcx>, expected_ty: Ty<'tcx>, ) -> bool { - if self.tcx.hir().is_const_context(expr.hir_id) { - // Shouldn't suggest `.into()` on `const`s. - // FIXME(estebank): modify once we decide to suggest `as` casts - return false; - } + // Shouldn't suggest `.into()` on `const`s. + // FIXME(estebank): modify once we decide to suggest `as` casts + let is_const_context = self.tcx.hir().is_const_context(expr.hir_id); if self.tcx.sess.source_map().is_imported(expr.span) { // Ignore if span is from within a macro. return false; @@ -728,24 +726,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let suggest_to_change_suffix_or_into = |err: &mut DiagnosticBuilder<'_>, is_fallible: bool| { let into_sugg = into_suggestion.clone(); - err.span_suggestion( - expr.span, - if literal_is_ty_suffixed(expr) { - &lit_msg - } else if is_fallible { - &try_msg - } else { - &msg - }, - if literal_is_ty_suffixed(expr) { - suffix_suggestion.clone() - } else if is_fallible { - try_into_suggestion - } else { - into_sugg - }, - Applicability::MachineApplicable, - ); + if !is_const_context || literal_is_ty_suffixed(expr) { + err.span_suggestion( + expr.span, + if literal_is_ty_suffixed(expr) { + &lit_msg + } else if is_fallible { + &try_msg + } else { + &msg + }, + if literal_is_ty_suffixed(expr) { + suffix_suggestion.clone() + } else if is_fallible { + try_into_suggestion + } else { + into_sugg + }, + Applicability::MachineApplicable, + ); + } }; match (&expected_ty.kind, &checked_ty.kind) { diff --git a/src/test/ui/discrim/discrim-ill-typed.stderr b/src/test/ui/discrim/discrim-ill-typed.stderr index b0e11ee5a6e60..7b9f086151a84 100644 --- a/src/test/ui/discrim/discrim-ill-typed.stderr +++ b/src/test/ui/discrim/discrim-ill-typed.stderr @@ -3,48 +3,88 @@ error[E0308]: mismatched types | LL | OhNo = 0_u8, | ^^^^ expected `i8`, found `u8` + | +help: change the type of the numeric literal from `u8` to `i8` + | +LL | OhNo = 0_i8, + | ^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:30:16 | LL | OhNo = 0_i8, | ^^^^ expected `u8`, found `i8` + | +help: change the type of the numeric literal from `i8` to `u8` + | +LL | OhNo = 0_u8, + | ^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:43:16 | LL | OhNo = 0_u16, | ^^^^^ expected `i16`, found `u16` + | +help: change the type of the numeric literal from `u16` to `i16` + | +LL | OhNo = 0_i16, + | ^^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:56:16 | LL | OhNo = 0_i16, | ^^^^^ expected `u16`, found `i16` + | +help: change the type of the numeric literal from `i16` to `u16` + | +LL | OhNo = 0_u16, + | ^^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:69:16 | LL | OhNo = 0_u32, | ^^^^^ expected `i32`, found `u32` + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | OhNo = 0_i32, + | ^^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:82:16 | LL | OhNo = 0_i32, | ^^^^^ expected `u32`, found `i32` + | +help: change the type of the numeric literal from `i32` to `u32` + | +LL | OhNo = 0_u32, + | ^^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:95:16 | LL | OhNo = 0_u64, | ^^^^^ expected `i64`, found `u64` + | +help: change the type of the numeric literal from `u64` to `i64` + | +LL | OhNo = 0_i64, + | ^^^^^ error[E0308]: mismatched types --> $DIR/discrim-ill-typed.rs:108:16 | LL | OhNo = 0_i64, | ^^^^^ expected `u64`, found `i64` + | +help: change the type of the numeric literal from `i64` to `u64` + | +LL | OhNo = 0_u64, + | ^^^^^ error: aborting due to 8 previous errors diff --git a/src/test/ui/issues/issue-8761.stderr b/src/test/ui/issues/issue-8761.stderr index 6ab74a9e98940..836520a28ef34 100644 --- a/src/test/ui/issues/issue-8761.stderr +++ b/src/test/ui/issues/issue-8761.stderr @@ -3,12 +3,22 @@ error[E0308]: mismatched types | LL | A = 1i64, | ^^^^ expected `isize`, found `i64` + | +help: change the type of the numeric literal from `i64` to `isize` + | +LL | A = 1isize, + | ^^^^^^ error[E0308]: mismatched types --> $DIR/issue-8761.rs:5:9 | LL | B = 2u8 | ^^^ expected `isize`, found `u8` + | +help: change the type of the numeric literal from `u8` to `isize` + | +LL | B = 2isize + | ^^^^^^ error: aborting due to 2 previous errors diff --git a/src/test/ui/numeric/const-scope.stderr b/src/test/ui/numeric/const-scope.stderr index 6e1990e3a7222..d7f18e19b41bd 100644 --- a/src/test/ui/numeric/const-scope.stderr +++ b/src/test/ui/numeric/const-scope.stderr @@ -3,6 +3,11 @@ error[E0308]: mismatched types | LL | const C: i32 = 1i8; | ^^^ expected `i32`, found `i8` + | +help: change the type of the numeric literal from `i8` to `i32` + | +LL | const C: i32 = 1i32; + | ^^^^ error[E0308]: mismatched types --> $DIR/const-scope.rs:2:15 @@ -17,6 +22,11 @@ LL | let c: i32 = 1i8; | --- ^^^ expected `i32`, found `i8` | | | expected due to this + | +help: change the type of the numeric literal from `i8` to `i32` + | +LL | let c: i32 = 1i32; + | ^^^^ error[E0308]: mismatched types --> $DIR/const-scope.rs:6:17