Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions compiler/rustc_attr_ir/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,9 +1646,6 @@ pub enum AttributeKind {
/// Represents `#[rustc_strict_coherence]`.
RustcStrictCoherence(Span),

/// Represents `#[rustc_test_entrypoint_marker]`
RustcTestEntrypointMarker,

/// Represents `#[rustc_test_marker]`
RustcTestMarker(Symbol),

Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_attr_ir/src/encode_cross_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ impl AttributeKind {
RustcSpecializationTrait => No,
RustcStdInternalSymbol => No,
RustcStrictCoherence(..) => Yes,
RustcTestEntrypointMarker => No,
RustcTestMarker(..) => No,
RustcThenThisWouldNeed(..) => No,
RustcTrivialFieldReads => Yes,
Expand Down
11 changes: 0 additions & 11 deletions compiler/rustc_attr_parsing/src/attributes/test_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,3 @@ impl SingleAttributeParser for RustcTestMarkerParser {
Some(AttributeKind::RustcTestMarker(value_str))
}
}

pub(crate) struct RustcTestEntrypointMarkerParser;

impl NoArgsAttributeParser for RustcTestEntrypointMarkerParser {
const PATH: &[Symbol] = &[sym::rustc_test_entrypoint_marker];
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowList(&[Allow(Target::Fn), Allow(Target::Closure)]);
const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
const STABILITY: AttributeStability = unstable!(rustc_attrs);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcTestEntrypointMarker;
}
1 change: 0 additions & 1 deletion compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ attribute_parsers!(
Single<WithoutArgs<RustcSpecializationTraitParser>>,
Single<WithoutArgs<RustcStdInternalSymbolParser>>,
Single<WithoutArgs<RustcStrictCoherenceParser>>,
Single<WithoutArgs<RustcTestEntrypointMarkerParser>>,
Single<WithoutArgs<RustcTrivialFieldReadsParser>>,
Single<WithoutArgs<SplatParser>>,
Single<WithoutArgs<ThreadLocalParser>>,
Expand Down
130 changes: 2 additions & 128 deletions compiler/rustc_builtin_macros/src/deriving/debug.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_ast::{self as ast, EnumDef, ExprKind, MetaItem, Safety, TyKind, token};
use rustc_ast::{self as ast, EnumDef, MetaItem, Safety};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_session::config::FmtDebug;
use rustc_span::{Ident, Span, Symbol, sym};
Expand Down Expand Up @@ -230,10 +230,6 @@ fn show_fieldless_enum(
substr: &Substructure<'_>,
) -> BlockOrExpr {
let fmt = substr.nonselflike_args[0].clone();
if let Some((stmts, expr)) = show_fieldless_enum_concat_str(cx, span, def, fmt.clone()) {
return BlockOrExpr::new_mixed(stmts, Some(expr));
}
let fn_path_write_str = cx.std_path(&[sym::fmt, sym::Formatter, sym::write_str]);
let arms = def
.variants
.iter()
Expand All @@ -254,128 +250,6 @@ fn show_fieldless_enum(
})
.collect::<ThinVec<_>>();
let name = cx.expr_match(span, cx.expr_self(span), arms);
let fn_path_write_str = cx.std_path(&[sym::fmt, sym::Formatter, sym::write_str]);
BlockOrExpr::new_expr(cx.expr_call_global(span, fn_path_write_str, thin_vec![fmt, name]))
}

/// Special case for fieldless enums with no discriminants. Builds
/// ```text
/// impl ::core::fmt::Debug for A {
/// fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
/// static __NAMES: &str = "ABBBCC";
/// static __OFFSET: [usize; 4] =[0, 1, 4, 6];
/// let __d = ::core::intrinsics::discriminant_value(self) as usize;
/// ::core::fmt::Formatter::debug_c_like_enums_write_str(f, __NAMES, &__OFFSET, __d)
/// }
/// }
/// ```
fn show_fieldless_enum_concat_str(
cx: &ExtCtxt<'_>,
span: Span,
def: &EnumDef,
fmt: Box<ast::Expr>,
) -> Option<(ThinVec<ast::Stmt>, Box<ast::Expr>)> {
// Minimum variants count where this optimization starts to pay off.
// See https://github.com/rust-lang/rust/pull/155452 for more details.
const THRESHOLD: usize = 10;
let variants_count = def.variants.len();
if variants_count < THRESHOLD {
return None;
}

let variant_names = def
.variants
.iter()
.map(|v| v.disr_expr.is_none().then_some(v.ident.name.as_str()))
.collect::<Option<ThinVec<_>>>()?;

let total_bytes: usize = variant_names.iter().map(|n| n.len()).sum();
let mut concatenated_names = String::with_capacity(total_bytes);
let mut offset_indices = Vec::with_capacity(variant_names.len() + 1);
offset_indices.push(0);

for name in variant_names.iter() {
concatenated_names.push_str(name);
offset_indices.push(concatenated_names.len());
}

// Create the constant concatenated string
let names_ident = Ident::from_str_and_span("__NAMES", span);
let str_ty = cx.ty(
span,
TyKind::Ref(
None,
ast::MutTy {
ty: cx.ty(
span,
TyKind::Path(None, ast::Path::from_ident(Ident::new(sym::str, span))),
),
mutbl: ast::Mutability::Not,
},
),
);
let names_str_body = cx.expr_str(span, Symbol::intern(&concatenated_names));
let names_static_item =
cx.item_static(span, names_ident, str_ty, ast::Mutability::Not, names_str_body);

// Create the constant offset array
let offset_ident = Ident::from_str_and_span("__OFFSET", span);
let offset_index_exprs =
offset_indices.iter().map(|s| cx.expr_usize(span, *s)).collect::<ThinVec<_>>();
let starts_array_body = cx.expr_array(span, offset_index_exprs);
let usize_ty =
cx.ty(span, TyKind::Path(None, ast::Path::from_ident(Ident::new(sym::usize, span))));
let offset_array_len_expr = cx.anon_const(
span,
ExprKind::Lit(token::Lit::new(
token::LitKind::Integer,
Symbol::intern(&(variants_count + 1).to_string()),
None,
)),
);
let offset_static_item = cx.item_static(
span,
offset_ident,
cx.ty(span, TyKind::Array(usize_ty, offset_array_len_expr)),
ast::Mutability::Not,
starts_array_body,
);

// let __d = ::core::intrinsics::discriminant_value(self) as usize;
let discriminant_ident = Ident::from_str_and_span("__d", span);
let discriminant_intrinsic_path = cx.std_path(&[sym::intrinsics, sym::discriminant_value]);
let discriminant_cast_expr = cx.expr(
span,
ast::ExprKind::Cast(
cx.expr_call_global(span, discriminant_intrinsic_path, thin_vec![cx.expr_self(span)]),
cx.ty_path(ast::Path::from_ident(Ident::new(sym::usize, span))),
),
);
let discriminant_let_stmt =
cx.stmt_let(span, false, discriminant_ident, discriminant_cast_expr);

// __d expression
let discriminant_expr = cx.expr_ident(span, discriminant_ident);

// __NAMES expression
let names_expr = cx.expr_ident(span, names_ident);

// &__OFFSET expression
let offset_ref_expr = cx.expr_addr_of(span, cx.expr_ident(span, offset_ident));

// ::core::fmt::Formatter::debug_c_like_enum_write_str(f, __NAMES, &__OFFSET, __d)
let fn_path = cx.std_path(&[sym::fmt, sym::Formatter, sym::debug_c_like_enum_write_str]);
let call_expr = cx.expr_call_global(
span,
fn_path,
thin_vec![fmt, names_expr, offset_ref_expr, discriminant_expr],
);

Some((
thin_vec![
cx.stmt_item(span, names_static_item),
cx.stmt_item(span, offset_static_item),
discriminant_let_stmt,
],
call_expr,
))
}
9 changes: 0 additions & 9 deletions compiler/rustc_builtin_macros/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ use crate::util::{check_builtin_macro_attribute, warn_on_duplicate_attribute};
///
/// We mark item with an inert attribute "rustc_test_marker" which the test generation
/// logic will pick up on.
///
/// The test function also gains a `#[rustc_test_entrypoint_marker]` attribute for tools to pick up
/// on. This behavior is *unstable*.
pub(crate) fn expand_test_case(
ecx: &mut ExtCtxt<'_>,
attr_sp: Span,
Expand Down Expand Up @@ -380,12 +377,6 @@ pub(crate) fn expand_test_or_bench(
let test_extern =
cx.item(sp, ast::AttrVec::new(), ast::ItemKind::ExternCrate(None, test_ident));

let item = {
let mut item = item;
item.attrs.push(cx.attr_word(sym::rustc_test_entrypoint_marker, attr_sp));
item
};

debug!("synthetic test item:\n{}\n", pprust::item_to_string(&test_const));

if is_stmt {
Expand Down
14 changes: 13 additions & 1 deletion compiler/rustc_codegen_llvm/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ffi::CString;
use std::ffi::{CString, c_uint};
use std::path::Path;

use rustc_data_structures::small_c_str::SmallCStr;
Expand Down Expand Up @@ -265,3 +265,15 @@ pub(crate) struct IntrinsicWrongArch<'a> {
pub(crate) struct UnknownLlvmTargetFeaturePrefix<'a> {
pub feature: &'a str,
}

#[derive(Diagnostic)]
#[diag(
"LLVM version mismatch: this compiler was built for LLVM {$expected_version}, but LLVM {$llvm_major}.{$llvm_minor}.{$llvm_patch} was found{$dll_loc}"
)]
pub(crate) struct LlvmVersionMismatch<'a> {
pub expected_version: c_uint,
pub llvm_major: c_uint,
pub llvm_minor: c_uint,
pub llvm_patch: c_uint,
pub dll_loc: &'a str,
}
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,8 @@ unsafe extern "C" {
SLen: c_uint,
) -> MetadataKindId;

pub(crate) fn LLVMGetVersion(major: &mut c_uint, minor: &mut c_uint, patch: &mut c_uint);

pub(crate) fn LLVMDisposeTargetMachine(T: ptr::NonNull<TargetMachine>);

// Create modules.
Expand Down
22 changes: 22 additions & 0 deletions compiler/rustc_codegen_llvm/src/llvm_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ unsafe fn configure_llvm(sess: &Session) {
let mut llvm_c_strs = Vec::with_capacity(n_args + 1);
let mut llvm_args = Vec::with_capacity(n_args + 1);

// Check to ensure we're running against the correct LLVM version.
unsafe {
let mut llvm_major = 0;
let mut llvm_minor = 0;
let mut llvm_patch = 0;
llvm::LLVMGetVersion(&mut llvm_major, &mut llvm_minor, &mut llvm_patch);
let expected_version = llvm::LLVMRustVersionMajor();
if llvm_major != expected_version {
sess.dcx().emit_fatal(diagnostics::LlvmVersionMismatch {
expected_version,
llvm_major,
llvm_minor,
llvm_patch,
dll_loc: &match rustc_session::filesearch::dll_path(llvm::LLVMGetVersion as *mut _)
{
Ok(path) => format!(" at {}", path.display()),
Err(_) => String::new(),
},
})
}
}

unsafe {
llvm::LLVMRustInstallErrorHandlers();
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ pub static BUILTIN_ATTRIBUTES: &[Symbol] = &[
sym::rustc_paren_sugar,
sym::rustc_inherit_overflow_checks,
sym::rustc_reservation_impl,
sym::rustc_test_entrypoint_marker,
sym::rustc_test_marker,
sym::rustc_allow_lifetime_dependent_specialization,
sym::rustc_specialization_trait,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_macros/src/diagnostics/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ const ALLOWED_CAPITALIZED_WORDS: &[&str] = &[
"Cargo",
"Ferris",
"GCC",
"LLVM",
"MIR",
"NaNs",
"OK",
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
AttributeKind::RustcSpecializationTrait => (),
AttributeKind::RustcStdInternalSymbol => (),
AttributeKind::RustcStrictCoherence(..) => (),
AttributeKind::RustcTestEntrypointMarker => (),
AttributeKind::RustcTestMarker(..) => (),
AttributeKind::RustcThenThisWouldNeed(..) => (),
AttributeKind::RustcTrivialFieldReads => (),
Expand Down
Loading
Loading