diff options
| author | bors <bors@rust-lang.org> | 2021-01-10 08:01:12 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-01-10 08:01:12 +0000 |
| commit | 34628e5b533d35840b61c5db0665cf7633ed3c5a (patch) | |
| tree | bbdf4e4be0d4a44f172cf0528b45025d73300281 /compiler/rustc_resolve/src | |
| parent | 7a193921a024e910262ff90bfb028074fddf20d0 (diff) | |
| parent | 19b8c65e4e0af841a9ecaa99b5e1ae3c73ff9c7f (diff) | |
| download | rust-34628e5b533d35840b61c5db0665cf7633ed3c5a.tar.gz rust-34628e5b533d35840b61c5db0665cf7633ed3c5a.zip | |
Auto merge of #80867 - JohnTitor:rollup-tvqw555, r=JohnTitor
Rollup of 9 pull requests Successful merges: - #79502 (Implement From<char> for u64 and u128.) - #79968 (Improve core::ptr::drop_in_place debuginfo) - #80774 (Fix safety comment) - #80801 (Use correct span for structured suggestion) - #80803 (Remove useless `fill_in` function) - #80820 (Support `download-ci-llvm` on NixOS) - #80825 (Remove under-used ImplPolarity enum) - #80850 (Allow #[rustc_builtin_macro = "name"]) - #80857 (Add comment to `Vec::truncate` explaining `>` vs `>=`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_resolve/src')
| -rw-r--r-- | compiler/rustc_resolve/src/diagnostics.rs | 26 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/lib.rs | 25 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/macros.rs | 6 |
3 files changed, 36 insertions, 21 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 6a181dbab5a..03b66a3f7b3 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -398,20 +398,30 @@ impl<'a> Resolver<'a> { err.help("use the `|| { ... }` closure form instead"); err } - ResolutionError::AttemptToUseNonConstantValueInConstant(ident, sugg) => { + ResolutionError::AttemptToUseNonConstantValueInConstant(ident, sugg, current) => { let mut err = struct_span_err!( self.session, span, E0435, "attempt to use a non-constant value in a constant" ); - err.span_suggestion( - ident.span, - &sugg, - "".to_string(), - Applicability::MaybeIncorrect, - ); - err.span_label(span, "non-constant value"); + // let foo =... + // ^^^ given this Span + // ------- get this Span to have an applicable suggestion + let sp = + self.session.source_map().span_extend_to_prev_str(ident.span, current, true); + if sp.lo().0 == 0 { + err.span_label(ident.span, &format!("this would need to be a `{}`", sugg)); + } else { + let sp = sp.with_lo(BytePos(sp.lo().0 - current.len() as u32)); + err.span_suggestion( + sp, + &format!("consider using `{}` instead of `{}`", sugg, current), + format!("{} {}", sugg, ident), + Applicability::MaybeIncorrect, + ); + err.span_label(span, "non-constant value"); + } err } ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index a6d0240b6fd..bc6c70c26ae 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -210,7 +210,11 @@ enum ResolutionError<'a> { /// Error E0434: can't capture dynamic environment in a fn item. CannotCaptureDynamicEnvironmentInFnItem, /// Error E0435: attempt to use a non-constant value in a constant. - AttemptToUseNonConstantValueInConstant(Ident, String), + AttemptToUseNonConstantValueInConstant( + Ident, + /* suggestion */ &'static str, + /* current */ &'static str, + ), /// Error E0530: `X` bindings cannot shadow `Y`s. BindingShadowsSomethingUnacceptable(&'static str, Symbol, &'a NameBinding<'a>), /// Error E0128: type parameters with a default cannot use forward-declared identifiers. @@ -1443,7 +1447,7 @@ impl<'a> Resolver<'a> { } fn is_builtin_macro(&mut self, res: Res) -> bool { - self.get_macro(res).map_or(false, |ext| ext.is_builtin) + self.get_macro(res).map_or(false, |ext| ext.builtin_name.is_some()) } fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId { @@ -2010,7 +2014,7 @@ impl<'a> Resolver<'a> { // The macro is a proc macro derive if let Some(def_id) = module.expansion.expn_data().macro_def_id { let ext = self.get_macro_by_def_id(def_id); - if !ext.is_builtin + if ext.builtin_name.is_none() && ext.macro_kind() == MacroKind::Derive && parent.expansion.outer_expn_is_descendant_of(span.ctxt()) { @@ -2614,18 +2618,19 @@ impl<'a> Resolver<'a> { ConstantItemKind::Const => "const", ConstantItemKind::Static => "static", }; - let sugg = format!( - "consider using `let` instead of `{}`", - kind_str - ); - (span, AttemptToUseNonConstantValueInConstant(ident, sugg)) + ( + span, + AttemptToUseNonConstantValueInConstant( + ident, "let", kind_str, + ), + ) } else { - let sugg = "consider using `const` instead of `let`"; ( rib_ident.span, AttemptToUseNonConstantValueInConstant( original_rib_ident_def, - sugg.to_string(), + "const", + "let", ), ) }; diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index e6360cccf3b..60a1fc88631 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -285,7 +285,7 @@ impl<'a> ResolverExpand for Resolver<'a> { helper_attrs.extend( ext.helper_attrs.iter().map(|name| Ident::new(*name, span)), ); - if ext.is_derive_copy { + if ext.builtin_name == Some(sym::Copy) { self.containers_deriving_copy.insert(invoc_id); } ext @@ -1089,9 +1089,9 @@ impl<'a> Resolver<'a> { edition, ); - if result.is_builtin { + if let Some(builtin_name) = result.builtin_name { // The macro was marked with `#[rustc_builtin_macro]`. - if let Some(builtin_macro) = self.builtin_macros.get_mut(&item.ident.name) { + if let Some(builtin_macro) = self.builtin_macros.get_mut(&builtin_name) { // The macro is a built-in, replace its expander function // while still taking everything else from the source code. // If we already loaded this builtin macro, give a better error message than 'no such builtin macro'. |
