summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_builtin_macros/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:44 +0000
commitc23a457e72abe608715ac76f076f47dc42af07a5 (patch)
tree2772049aaf84b5c9d0ed12ec8d86812f7a7904b6 /compiler/rustc_builtin_macros/src
parentReleasing progress-linux version 1.73.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-c23a457e72abe608715ac76f076f47dc42af07a5.tar.xz
rustc-c23a457e72abe608715ac76f076f47dc42af07a5.zip
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_builtin_macros/src')
-rw-r--r--compiler/rustc_builtin_macros/src/assert/context.rs2
-rw-r--r--compiler/rustc_builtin_macros/src/deriving/bounds.rs2
-rw-r--r--compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs19
-rw-r--r--compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs19
-rw-r--r--compiler/rustc_builtin_macros/src/deriving/generic/mod.rs34
-rw-r--r--compiler/rustc_builtin_macros/src/deriving/mod.rs98
-rw-r--r--compiler/rustc_builtin_macros/src/errors.rs35
-rw-r--r--compiler/rustc_builtin_macros/src/format.rs37
-rw-r--r--compiler/rustc_builtin_macros/src/source_util.rs2
-rw-r--r--compiler/rustc_builtin_macros/src/test_harness.rs8
10 files changed, 102 insertions, 154 deletions
diff --git a/compiler/rustc_builtin_macros/src/assert/context.rs b/compiler/rustc_builtin_macros/src/assert/context.rs
index bda473120..0682d48ac 100644
--- a/compiler/rustc_builtin_macros/src/assert/context.rs
+++ b/compiler/rustc_builtin_macros/src/assert/context.rs
@@ -241,7 +241,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
self.manage_cond_expr(prefix);
self.manage_cond_expr(suffix);
}
- ExprKind::Let(_, local_expr, _) => {
+ ExprKind::Let(_, local_expr, _, _) => {
self.manage_cond_expr(local_expr);
}
ExprKind::Match(local_expr, _) => {
diff --git a/compiler/rustc_builtin_macros/src/deriving/bounds.rs b/compiler/rustc_builtin_macros/src/deriving/bounds.rs
index 2c8e6f99c..8027ca2e7 100644
--- a/compiler/rustc_builtin_macros/src/deriving/bounds.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/bounds.rs
@@ -41,7 +41,7 @@ pub fn expand_deriving_const_param_ty(
path: path_std!(marker::ConstParamTy),
skip_path_as_bound: false,
needs_copy_as_bound_if_packed: false,
- additional_bounds: Vec::new(),
+ additional_bounds: vec![ty::Ty::Path(path_std!(cmp::Eq))],
supports_unions: false,
methods: Vec::new(),
associated_types: Vec::new(),
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
index c78a0eb04..a000e4895 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
@@ -18,6 +18,20 @@ pub fn expand_deriving_eq(
is_const: bool,
) {
let span = cx.with_def_site_ctxt(span);
+
+ let structural_trait_def = TraitDef {
+ span,
+ path: path_std!(marker::StructuralEq),
+ skip_path_as_bound: true, // crucial!
+ needs_copy_as_bound_if_packed: false,
+ additional_bounds: Vec::new(),
+ supports_unions: true,
+ methods: Vec::new(),
+ associated_types: Vec::new(),
+ is_const: false,
+ };
+ structural_trait_def.expand(cx, mitem, item, push);
+
let trait_def = TraitDef {
span,
path: path_std!(cmp::Eq),
@@ -34,7 +48,7 @@ pub fn expand_deriving_eq(
attributes: thin_vec![
cx.attr_word(sym::inline, span),
cx.attr_nested_word(sym::doc, sym::hidden, span),
- cx.attr_word(sym::no_coverage, span)
+ cx.attr_nested_word(sym::coverage, sym::off, span)
],
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
combine_substructure: combine_substructure(Box::new(|a, b, c| {
@@ -44,9 +58,6 @@ pub fn expand_deriving_eq(
associated_types: Vec::new(),
is_const,
};
-
- super::inject_impl_of_structural_trait(cx, span, item, path_std!(marker::StructuralEq), push);
-
trait_def.expand_ext(cx, mitem, item, push, true)
}
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
index a71ecc5db..a170468b4 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
@@ -72,13 +72,20 @@ pub fn expand_deriving_partial_eq(
BlockOrExpr::new_expr(expr)
}
- super::inject_impl_of_structural_trait(
- cx,
+ let structural_trait_def = TraitDef {
span,
- item,
- path_std!(marker::StructuralPartialEq),
- push,
- );
+ path: path_std!(marker::StructuralPartialEq),
+ skip_path_as_bound: true, // crucial!
+ needs_copy_as_bound_if_packed: false,
+ additional_bounds: Vec::new(),
+ // We really don't support unions, but that's already checked by the impl generated below;
+ // a second check here would lead to redundant error messages.
+ supports_unions: true,
+ methods: Vec::new(),
+ associated_types: Vec::new(),
+ is_const: false,
+ };
+ structural_trait_def.expand(cx, mitem, item, push);
// No need to generate `ne`, the default suffices, and not generating it is
// faster.
diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
index 6597ee3cf..7252658d4 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
@@ -88,7 +88,7 @@
//!
//! When generating the `expr` for the `A` impl, the `SubstructureFields` is
//!
-//! ```{.text}
+//! ```text
//! Struct(vec![FieldInfo {
//! span: <span of x>
//! name: Some(<ident of x>),
@@ -99,7 +99,7 @@
//!
//! For the `B` impl, called with `B(a)` and `B(b)`,
//!
-//! ```{.text}
+//! ```text
//! Struct(vec![FieldInfo {
//! span: <span of `i32`>,
//! name: None,
@@ -113,7 +113,7 @@
//! When generating the `expr` for a call with `self == C0(a)` and `other
//! == C0(b)`, the SubstructureFields is
//!
-//! ```{.text}
+//! ```text
//! EnumMatching(0, <ast::Variant for C0>,
//! vec![FieldInfo {
//! span: <span of i32>
@@ -125,7 +125,7 @@
//!
//! For `C1 {x}` and `C1 {x}`,
//!
-//! ```{.text}
+//! ```text
//! EnumMatching(1, <ast::Variant for C1>,
//! vec![FieldInfo {
//! span: <span of x>
@@ -137,7 +137,7 @@
//!
//! For the tags,
//!
-//! ```{.text}
+//! ```text
//! EnumTag(
//! &[<ident of self tag>, <ident of other tag>], <expr to combine with>)
//! ```
@@ -149,7 +149,7 @@
//!
//! A static method on the types above would result in,
//!
-//! ```{.text}
+//! ```text
//! StaticStruct(<ast::VariantData of A>, Named(vec![(<ident of x>, <span of x>)]))
//!
//! StaticStruct(<ast::VariantData of B>, Unnamed(vec![<span of x>]))
@@ -711,7 +711,9 @@ impl<'a> TraitDef<'a> {
.collect();
// Require the current trait.
- bounds.push(cx.trait_bound(trait_path.clone(), self.is_const));
+ if !self.skip_path_as_bound {
+ bounds.push(cx.trait_bound(trait_path.clone(), self.is_const));
+ }
// Add a `Copy` bound if required.
if is_packed && self.needs_copy_as_bound_if_packed {
@@ -722,15 +724,17 @@ impl<'a> TraitDef<'a> {
));
}
- let predicate = ast::WhereBoundPredicate {
- span: self.span,
- bound_generic_params: field_ty_param.bound_generic_params,
- bounded_ty: field_ty_param.ty,
- bounds,
- };
+ if !bounds.is_empty() {
+ let predicate = ast::WhereBoundPredicate {
+ span: self.span,
+ bound_generic_params: field_ty_param.bound_generic_params,
+ bounded_ty: field_ty_param.ty,
+ bounds,
+ };
- let predicate = ast::WherePredicate::BoundPredicate(predicate);
- where_clause.predicates.push(predicate);
+ let predicate = ast::WherePredicate::BoundPredicate(predicate);
+ where_clause.predicates.push(predicate);
+ }
}
}
}
diff --git a/compiler/rustc_builtin_macros/src/deriving/mod.rs b/compiler/rustc_builtin_macros/src/deriving/mod.rs
index d34336e76..a6f3252e7 100644
--- a/compiler/rustc_builtin_macros/src/deriving/mod.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/mod.rs
@@ -2,9 +2,9 @@
use rustc_ast as ast;
use rustc_ast::ptr::P;
-use rustc_ast::{GenericArg, Impl, ItemKind, MetaItem};
+use rustc_ast::{GenericArg, MetaItem};
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, MultiItemModifier};
-use rustc_span::symbol::{sym, Ident, Symbol};
+use rustc_span::symbol::{sym, Symbol};
use rustc_span::Span;
use thin_vec::{thin_vec, ThinVec};
@@ -116,100 +116,6 @@ fn call_unreachable(cx: &ExtCtxt<'_>, span: Span) -> P<ast::Expr> {
}))
}
-// Injects `impl<...> Structural for ItemType<...> { }`. In particular,
-// does *not* add `where T: Structural` for parameters `T` in `...`.
-// (That's the main reason we cannot use TraitDef here.)
-fn inject_impl_of_structural_trait(
- cx: &mut ExtCtxt<'_>,
- span: Span,
- item: &Annotatable,
- structural_path: generic::ty::Path,
- push: &mut dyn FnMut(Annotatable),
-) {
- let Annotatable::Item(item) = item else {
- unreachable!();
- };
-
- let generics = match &item.kind {
- ItemKind::Struct(_, generics) | ItemKind::Enum(_, generics) => generics,
- // Do not inject `impl Structural for Union`. (`PartialEq` does not
- // support unions, so we will see error downstream.)
- ItemKind::Union(..) => return,
- _ => unreachable!(),
- };
-
- // Create generics param list for where clauses and impl headers
- let mut generics = generics.clone();
-
- let ctxt = span.ctxt();
-
- // Create the type of `self`.
- //
- // in addition, remove defaults from generic params (impls cannot have them).
- let self_params: Vec<_> = generics
- .params
- .iter_mut()
- .map(|param| match &mut param.kind {
- ast::GenericParamKind::Lifetime => ast::GenericArg::Lifetime(
- cx.lifetime(param.ident.span.with_ctxt(ctxt), param.ident),
- ),
- ast::GenericParamKind::Type { default } => {
- *default = None;
- ast::GenericArg::Type(cx.ty_ident(param.ident.span.with_ctxt(ctxt), param.ident))
- }
- ast::GenericParamKind::Const { ty: _, kw_span: _, default } => {
- *default = None;
- ast::GenericArg::Const(
- cx.const_ident(param.ident.span.with_ctxt(ctxt), param.ident),
- )
- }
- })
- .collect();
-
- let type_ident = item.ident;
-
- let trait_ref = cx.trait_ref(structural_path.to_path(cx, span, type_ident, &generics));
- let self_type = cx.ty_path(cx.path_all(span, false, vec![type_ident], self_params));
-
- // It would be nice to also encode constraint `where Self: Eq` (by adding it
- // onto `generics` cloned above). Unfortunately, that strategy runs afoul of
- // rust-lang/rust#48214. So we perform that additional check in the compiler
- // itself, instead of encoding it here.
-
- // Keep the lint and stability attributes of the original item, to control
- // how the generated implementation is linted.
- let mut attrs = ast::AttrVec::new();
- attrs.extend(
- item.attrs
- .iter()
- .filter(|a| {
- [sym::allow, sym::warn, sym::deny, sym::forbid, sym::stable, sym::unstable]
- .contains(&a.name_or_empty())
- })
- .cloned(),
- );
- // Mark as `automatically_derived` to avoid some silly lints.
- attrs.push(cx.attr_word(sym::automatically_derived, span));
-
- let newitem = cx.item(
- span,
- Ident::empty(),
- attrs,
- ItemKind::Impl(Box::new(Impl {
- unsafety: ast::Unsafe::No,
- polarity: ast::ImplPolarity::Positive,
- defaultness: ast::Defaultness::Final,
- constness: ast::Const::No,
- generics,
- of_trait: Some(trait_ref),
- self_ty: self_type,
- items: ThinVec::new(),
- })),
- );
-
- push(Annotatable::Item(newitem));
-}
-
fn assert_ty_bounds(
cx: &mut ExtCtxt<'_>,
stmts: &mut ThinVec<ast::Stmt>,
diff --git a/compiler/rustc_builtin_macros/src/errors.rs b/compiler/rustc_builtin_macros/src/errors.rs
index fbf0395bb..1238773d5 100644
--- a/compiler/rustc_builtin_macros/src/errors.rs
+++ b/compiler/rustc_builtin_macros/src/errors.rs
@@ -539,18 +539,29 @@ pub(crate) struct InvalidFormatStringLabel {
}
#[derive(Subdiagnostic)]
-#[multipart_suggestion(
- builtin_macros_sugg,
- style = "verbose",
- applicability = "machine-applicable"
-)]
-pub(crate) struct InvalidFormatStringSuggestion {
- #[suggestion_part(code = "{len}")]
- pub(crate) captured: Span,
- pub(crate) len: String,
- #[suggestion_part(code = ", {arg}")]
- pub(crate) span: Span,
- pub(crate) arg: String,
+pub(crate) enum InvalidFormatStringSuggestion {
+ #[multipart_suggestion(
+ builtin_macros_format_use_positional,
+ style = "verbose",
+ applicability = "machine-applicable"
+ )]
+ UsePositional {
+ #[suggestion_part(code = "{len}")]
+ captured: Span,
+ len: String,
+ #[suggestion_part(code = ", {arg}")]
+ span: Span,
+ arg: String,
+ },
+ #[suggestion(
+ builtin_macros_format_remove_raw_ident,
+ code = "",
+ applicability = "machine-applicable"
+ )]
+ RemoveRawIdent {
+ #[primary_span]
+ span: Span,
+ },
}
#[derive(Diagnostic)]
diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs
index ede95dbf8..8397b5e42 100644
--- a/compiler/rustc_builtin_macros/src/format.rs
+++ b/compiler/rustc_builtin_macros/src/format.rs
@@ -260,20 +260,29 @@ fn make_format_args(
if let Some((label, span)) = err.secondary_label && is_source_literal {
e.label_ = Some(errors::InvalidFormatStringLabel { span: fmt_span.from_inner(InnerSpan::new(span.start, span.end)), label } );
}
- if err.should_be_replaced_with_positional_argument {
- let captured_arg_span =
- fmt_span.from_inner(InnerSpan::new(err.span.start, err.span.end));
- if let Ok(arg) = ecx.source_map().span_to_snippet(captured_arg_span) {
- let span = match args.unnamed_args().last() {
- Some(arg) => arg.expr.span,
- None => fmt_span,
- };
- e.sugg_ = Some(errors::InvalidFormatStringSuggestion {
- captured: captured_arg_span,
- len: args.unnamed_args().len().to_string(),
- span: span.shrink_to_hi(),
- arg,
- });
+ match err.suggestion {
+ parse::Suggestion::None => {}
+ parse::Suggestion::UsePositional => {
+ let captured_arg_span =
+ fmt_span.from_inner(InnerSpan::new(err.span.start, err.span.end));
+ if let Ok(arg) = ecx.source_map().span_to_snippet(captured_arg_span) {
+ let span = match args.unnamed_args().last() {
+ Some(arg) => arg.expr.span,
+ None => fmt_span,
+ };
+ e.sugg_ = Some(errors::InvalidFormatStringSuggestion::UsePositional {
+ captured: captured_arg_span,
+ len: args.unnamed_args().len().to_string(),
+ span: span.shrink_to_hi(),
+ arg,
+ });
+ }
+ }
+ parse::Suggestion::RemoveRawIdent(span) => {
+ if is_source_literal {
+ let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
+ e.sugg_ = Some(errors::InvalidFormatStringSuggestion::RemoveRawIdent { span })
+ }
}
}
ecx.emit_err(e);
diff --git a/compiler/rustc_builtin_macros/src/source_util.rs b/compiler/rustc_builtin_macros/src/source_util.rs
index 433da7423..953d957a4 100644
--- a/compiler/rustc_builtin_macros/src/source_util.rs
+++ b/compiler/rustc_builtin_macros/src/source_util.rs
@@ -217,7 +217,7 @@ pub fn expand_include_bytes(
};
match cx.source_map().load_binary_file(&file) {
Ok(bytes) => {
- let expr = cx.expr(sp, ast::ExprKind::IncludedBytes(bytes.into()));
+ let expr = cx.expr(sp, ast::ExprKind::IncludedBytes(bytes));
base::MacEager::expr(expr)
}
Err(e) => {
diff --git a/compiler/rustc_builtin_macros/src/test_harness.rs b/compiler/rustc_builtin_macros/src/test_harness.rs
index d8846a9f0..53ff089d7 100644
--- a/compiler/rustc_builtin_macros/src/test_harness.rs
+++ b/compiler/rustc_builtin_macros/src/test_harness.rs
@@ -254,7 +254,7 @@ fn generate_test_harness(
let expn_id = ext_cx.resolver.expansion_for_ast_pass(
DUMMY_SP,
AstPass::TestHarness,
- &[sym::test, sym::rustc_attrs, sym::no_coverage],
+ &[sym::test, sym::rustc_attrs, sym::coverage_attribute],
None,
);
let def_site = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
@@ -335,8 +335,8 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
// #[rustc_main]
let main_attr = ecx.attr_word(sym::rustc_main, sp);
- // #[no_coverage]
- let no_coverage_attr = ecx.attr_word(sym::no_coverage, sp);
+ // #[coverage(off)]
+ let coverage_attr = ecx.attr_nested_word(sym::coverage, sym::off, sp);
// pub fn main() { ... }
let main_ret_ty = ecx.ty(sp, ast::TyKind::Tup(ThinVec::new()));
@@ -366,7 +366,7 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
let main = P(ast::Item {
ident: main_id,
- attrs: thin_vec![main_attr, no_coverage_attr],
+ attrs: thin_vec![main_attr, coverage_attr],
id: ast::DUMMY_NODE_ID,
kind: main,
vis: ast::Visibility { span: sp, kind: ast::VisibilityKind::Public, tokens: None },