From 9ac48317b44f43e82875b8ff0477a436470ba418 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Fri, 8 Jan 2021 06:57:57 +0900 Subject: [PATCH] Revert "Rollup merge of #80538 - JulianKnodt:err_usize, r=lcnr" This reverts commit be2a3f86420f1efa95ee68b6a9f89b5a1e6f810b, reversing changes made to 4c4e8e70bb5f28c9ed7b0b30d70c80261871c8ec. --- Cargo.lock | 1 - compiler/rustc_hir/Cargo.toml | 1 - compiler/rustc_hir/src/hir.rs | 8 -- compiler/rustc_middle/src/ty/mod.rs | 9 -- compiler/rustc_typeck/src/astconv/generics.rs | 92 +++++++++---------- .../const-generics/suggest_const_for_array.rs | 10 -- .../suggest_const_for_array.stderr | 15 --- 7 files changed, 41 insertions(+), 95 deletions(-) delete mode 100644 src/test/ui/const-generics/suggest_const_for_array.rs delete mode 100644 src/test/ui/const-generics/suggest_const_for_array.stderr diff --git a/Cargo.lock b/Cargo.lock index 9a7797ed8814a..1a60de80e6877 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3745,7 +3745,6 @@ version = "0.0.0" dependencies = [ "rustc_ast", "rustc_data_structures", - "rustc_feature", "rustc_index", "rustc_macros", "rustc_serialize", diff --git a/compiler/rustc_hir/Cargo.toml b/compiler/rustc_hir/Cargo.toml index c14165454ed8f..b24c208c76aed 100644 --- a/compiler/rustc_hir/Cargo.toml +++ b/compiler/rustc_hir/Cargo.toml @@ -9,7 +9,6 @@ doctest = false [dependencies] rustc_target = { path = "../rustc_target" } -rustc_feature = { path = "../rustc_feature" } rustc_macros = { path = "../rustc_macros" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_index = { path = "../rustc_index" } diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index acd254ae85cb1..ead39d702635a 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -290,14 +290,6 @@ impl GenericArg<'_> { GenericArg::Const(_) => "const", } } - - pub fn to_ord(&self, feats: &rustc_feature::Features) -> ast::ParamKindOrd { - match self { - GenericArg::Lifetime(_) => ast::ParamKindOrd::Lifetime, - GenericArg::Type(_) => ast::ParamKindOrd::Type, - GenericArg::Const(_) => ast::ParamKindOrd::Const { unordered: feats.const_generics }, - } - } } #[derive(Debug, HashStable_Generic)] diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 59a3ac9463463..1fe1400fabecf 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -801,15 +801,6 @@ impl GenericParamDefKind { GenericParamDefKind::Const => "constant", } } - pub fn to_ord(&self, tcx: TyCtxt<'_>) -> ast::ParamKindOrd { - match self { - GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime, - GenericParamDefKind::Type { .. } => ast::ParamKindOrd::Type, - GenericParamDefKind::Const => { - ast::ParamKindOrd::Const { unordered: tcx.features().const_generics } - } - } - } } #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)] diff --git a/compiler/rustc_typeck/src/astconv/generics.rs b/compiler/rustc_typeck/src/astconv/generics.rs index 1100401ed12dd..a3a2b8967c606 100644 --- a/compiler/rustc_typeck/src/astconv/generics.rs +++ b/compiler/rustc_typeck/src/astconv/generics.rs @@ -11,7 +11,7 @@ use rustc_hir::GenericArg; use rustc_middle::ty::{ self, subst, subst::SubstsRef, GenericParamDef, GenericParamDefKind, Ty, TyCtxt, }; -use rustc_session::lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS; +use rustc_session::{lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS, Session}; use rustc_span::{symbol::kw, MultiSpan, Span}; use smallvec::SmallVec; @@ -20,72 +20,62 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { /// Report an error that a generic argument did not match the generic parameter that was /// expected. fn generic_arg_mismatch_err( - tcx: TyCtxt<'_>, + sess: &Session, arg: &GenericArg<'_>, - param: &GenericParamDef, + kind: &'static str, possible_ordering_error: bool, help: Option<&str>, ) { - let sess = tcx.sess; let mut err = struct_span_err!( sess, arg.span(), E0747, "{} provided when a {} was expected", arg.descr(), - param.kind.descr(), + kind, ); - if let GenericParamDefKind::Const { .. } = param.kind { + let unordered = sess.features_untracked().const_generics; + let kind_ord = match kind { + "lifetime" => ParamKindOrd::Lifetime, + "type" => ParamKindOrd::Type, + "constant" => ParamKindOrd::Const { unordered }, + // It's more concise to match on the string representation, though it means + // the match is non-exhaustive. + _ => bug!("invalid generic parameter kind {}", kind), + }; + + if let ParamKindOrd::Const { .. } = kind_ord { if let GenericArg::Type(hir::Ty { kind: hir::TyKind::Infer, .. }) = arg { err.help("const arguments cannot yet be inferred with `_`"); } } - // Specific suggestion set for diagnostics - match (arg, ¶m.kind) { - ( - GenericArg::Type(hir::Ty { kind: hir::TyKind::Path { .. }, .. }), - GenericParamDefKind::Const { .. }, - ) => { - let suggestions = vec![ - (arg.span().shrink_to_lo(), String::from("{ ")), - (arg.span().shrink_to_hi(), String::from(" }")), - ]; - err.multipart_suggestion( - "if this generic argument was intended as a const parameter, \ + let arg_ord = match arg { + GenericArg::Lifetime(_) => ParamKindOrd::Lifetime, + GenericArg::Type(_) => ParamKindOrd::Type, + GenericArg::Const(_) => ParamKindOrd::Const { unordered }, + }; + + if matches!(arg, GenericArg::Type(hir::Ty { kind: hir::TyKind::Path { .. }, .. })) + && matches!(kind_ord, ParamKindOrd::Const { .. }) + { + let suggestions = vec![ + (arg.span().shrink_to_lo(), String::from("{ ")), + (arg.span().shrink_to_hi(), String::from(" }")), + ]; + err.multipart_suggestion( + "if this generic argument was intended as a const parameter, \ try surrounding it with braces:", - suggestions, - Applicability::MaybeIncorrect, - ); - } - ( - GenericArg::Type(hir::Ty { kind: hir::TyKind::Array(_, len), .. }), - GenericParamDefKind::Const { .. }, - ) if tcx.type_of(param.def_id) == tcx.types.usize => { - let snippet = sess.source_map().span_to_snippet(tcx.hir().span(len.hir_id)); - if let Ok(snippet) = snippet { - err.span_suggestion( - arg.span(), - "array type provided where a `usize` was expected, try", - format!("{{ {} }}", snippet), - Applicability::MaybeIncorrect, - ); - } - } - _ => {} + suggestions, + Applicability::MaybeIncorrect, + ); } - let kind_ord = param.kind.to_ord(tcx); - let arg_ord = arg.to_ord(&tcx.features()); - // This note is only true when generic parameters are strictly ordered by their kind. if possible_ordering_error && kind_ord.cmp(&arg_ord) != core::cmp::Ordering::Equal { - let (first, last) = if kind_ord < arg_ord { - (param.kind.descr(), arg.descr()) - } else { - (arg.descr(), param.kind.descr()) - }; + let (first, last) = + if kind_ord < arg_ord { (kind, arg.descr()) } else { (arg.descr(), kind) }; err.note(&format!("{} arguments must be provided before {} arguments", first, last)); if let Some(help) = help { err.help(help); @@ -213,7 +203,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { // We expected a lifetime argument, but got a type or const // argument. That means we're inferring the lifetimes. substs.push(ctx.inferred_kind(None, param, infer_args)); - force_infer_lt = Some((arg, param)); + force_infer_lt = Some(arg); params.next(); } (GenericArg::Lifetime(_), _, ExplicitLateBound::Yes) => { @@ -223,7 +213,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { // ignore it. args.next(); } - (_, _, _) => { + (_, kind, _) => { // We expected one kind of parameter, but the user provided // another. This is an error. However, if we already know that // the arguments don't match up with the parameters, we won't issue @@ -266,9 +256,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { param_types_present.dedup(); Self::generic_arg_mismatch_err( - tcx, + tcx.sess, arg, - param, + kind.descr(), !args_iter.clone().is_sorted_by_key(|arg| match arg { GenericArg::Lifetime(_) => ParamKindOrd::Lifetime, GenericArg::Type(_) => ParamKindOrd::Type, @@ -325,9 +315,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { { let kind = arg.descr(); assert_eq!(kind, "lifetime"); - let (provided_arg, param) = + let provided = force_infer_lt.expect("lifetimes ought to have been inferred"); - Self::generic_arg_mismatch_err(tcx, provided_arg, param, false, None); + Self::generic_arg_mismatch_err(tcx.sess, provided, kind, false, None); } break; diff --git a/src/test/ui/const-generics/suggest_const_for_array.rs b/src/test/ui/const-generics/suggest_const_for_array.rs deleted file mode 100644 index f3e5a3186cdd6..0000000000000 --- a/src/test/ui/const-generics/suggest_const_for_array.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![crate_type = "lib"] - -fn example() {} - -fn other() { - example::<[usize; 3]>(); - //~^ ERROR type provided when a const - example::<[usize; 4+5]>(); - //~^ ERROR type provided when a const -} diff --git a/src/test/ui/const-generics/suggest_const_for_array.stderr b/src/test/ui/const-generics/suggest_const_for_array.stderr deleted file mode 100644 index a617bf2bb0d96..0000000000000 --- a/src/test/ui/const-generics/suggest_const_for_array.stderr +++ /dev/null @@ -1,15 +0,0 @@ -error[E0747]: type provided when a constant was expected - --> $DIR/suggest_const_for_array.rs:6:13 - | -LL | example::<[usize; 3]>(); - | ^^^^^^^^^^ help: array type provided where a `usize` was expected, try: `{ 3 }` - -error[E0747]: type provided when a constant was expected - --> $DIR/suggest_const_for_array.rs:8:13 - | -LL | example::<[usize; 4+5]>(); - | ^^^^^^^^^^^^ help: array type provided where a `usize` was expected, try: `{ 4+5 }` - -error: aborting due to 2 previous errors - -For more information about this error, try `rustc --explain E0747`.