Skip to content

Commit 826b673

Browse files
committed
Auto merge of #133377 - jieyouxu:rollup-n536hzq, r=jieyouxu
Rollup of 6 pull requests Successful merges: - #127483 (Allow disabling ASan instrumentation for globals) - #131505 (use `confstr(_CS_DARWIN_USER_TEMP_DIR, ...)` as a `TMPDIR` fallback on Darwin) - #132949 (Add specific diagnostic for using macro_rules macro as attribute/derive) - #133286 (Re-delay a resolve `bug` related to `Self`-ctor in patterns) - #133332 (Mark `<[T; N]>::as_mut_slice` with the `const` specifier.) - #133366 (Remove unnecessary bool from `ExpectedFound::new`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents ff1737b + 96e8c7c commit 826b673

File tree

37 files changed

+438
-141
lines changed

37 files changed

+438
-141
lines changed

compiler/rustc_codegen_llvm/src/base.rs

+9
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,12 @@ pub(crate) fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
172172
Visibility::Protected => llvm::Visibility::Protected,
173173
}
174174
}
175+
176+
pub(crate) fn set_variable_sanitizer_attrs(llval: &Value, attrs: &CodegenFnAttrs) {
177+
if attrs.no_sanitize.contains(SanitizerSet::ADDRESS) {
178+
unsafe { llvm::LLVMRustSetNoSanitizeAddress(llval) };
179+
}
180+
if attrs.no_sanitize.contains(SanitizerSet::HWADDRESS) {
181+
unsafe { llvm::LLVMRustSetNoSanitizeHWAddress(llval) };
182+
}
183+
}

compiler/rustc_codegen_llvm/src/consts.rs

+2
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ impl<'ll> CodegenCx<'ll, '_> {
470470
base::set_link_section(g, attrs);
471471
}
472472

473+
base::set_variable_sanitizer_attrs(g, attrs);
474+
473475
if attrs.flags.contains(CodegenFnAttrFlags::USED) {
474476
// `USED` and `USED_LINKER` can't be used together.
475477
assert!(!attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER));

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2460,4 +2460,7 @@ unsafe extern "C" {
24602460
pub fn LLVMRustIs64BitSymbolicFile(buf_ptr: *const u8, buf_len: usize) -> bool;
24612461

24622462
pub fn LLVMRustIsECObject(buf_ptr: *const u8, buf_len: usize) -> bool;
2463+
2464+
pub fn LLVMRustSetNoSanitizeAddress(Global: &Value);
2465+
pub fn LLVMRustSetNoSanitizeHWAddress(Global: &Value);
24632466
}

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
729729
let can_coerce = self.may_coerce(arg_ty, coerced_ty);
730730
if !can_coerce {
731731
return Compatibility::Incompatible(Some(ty::error::TypeError::Sorts(
732-
ty::error::ExpectedFound::new(true, coerced_ty, arg_ty),
732+
ty::error::ExpectedFound::new(coerced_ty, arg_ty),
733733
)));
734734
}
735735

@@ -758,7 +758,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
758758
} else {
759759
expected_ty
760760
};
761-
TypeTrace::types(&self.misc(span), true, mismatched_ty, provided_ty)
761+
TypeTrace::types(&self.misc(span), mismatched_ty, provided_ty)
762762
};
763763

764764
// The algorithm here is inspired by levenshtein distance and longest common subsequence.

compiler/rustc_infer/src/infer/at.rs

+13-29
Original file line numberDiff line numberDiff line change
@@ -308,25 +308,22 @@ impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
308308
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
309309
TypeTrace {
310310
cause: cause.clone(),
311-
values: ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into())),
311+
values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
312312
}
313313
}
314314
}
315315

316316
impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
317317
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
318-
TypeTrace {
319-
cause: cause.clone(),
320-
values: ValuePairs::Regions(ExpectedFound::new(true, a, b)),
321-
}
318+
TypeTrace { cause: cause.clone(), values: ValuePairs::Regions(ExpectedFound::new(a, b)) }
322319
}
323320
}
324321

325322
impl<'tcx> ToTrace<'tcx> for Const<'tcx> {
326323
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
327324
TypeTrace {
328325
cause: cause.clone(),
329-
values: ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into())),
326+
values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
330327
}
331328
}
332329
}
@@ -337,13 +334,13 @@ impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {
337334
cause: cause.clone(),
338335
values: match (a.unpack(), b.unpack()) {
339336
(GenericArgKind::Lifetime(a), GenericArgKind::Lifetime(b)) => {
340-
ValuePairs::Regions(ExpectedFound::new(true, a, b))
337+
ValuePairs::Regions(ExpectedFound::new(a, b))
341338
}
342339
(GenericArgKind::Type(a), GenericArgKind::Type(b)) => {
343-
ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into()))
340+
ValuePairs::Terms(ExpectedFound::new(a.into(), b.into()))
344341
}
345342
(GenericArgKind::Const(a), GenericArgKind::Const(b)) => {
346-
ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into()))
343+
ValuePairs::Terms(ExpectedFound::new(a.into(), b.into()))
347344
}
348345
_ => bug!("relating different kinds: {a:?} {b:?}"),
349346
},
@@ -353,37 +350,28 @@ impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {
353350

354351
impl<'tcx> ToTrace<'tcx> for ty::Term<'tcx> {
355352
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
356-
TypeTrace {
357-
cause: cause.clone(),
358-
values: ValuePairs::Terms(ExpectedFound::new(true, a, b)),
359-
}
353+
TypeTrace { cause: cause.clone(), values: ValuePairs::Terms(ExpectedFound::new(a, b)) }
360354
}
361355
}
362356

363357
impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
364358
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
365-
TypeTrace {
366-
cause: cause.clone(),
367-
values: ValuePairs::TraitRefs(ExpectedFound::new(true, a, b)),
368-
}
359+
TypeTrace { cause: cause.clone(), values: ValuePairs::TraitRefs(ExpectedFound::new(a, b)) }
369360
}
370361
}
371362

372363
impl<'tcx> ToTrace<'tcx> for ty::AliasTy<'tcx> {
373364
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
374365
TypeTrace {
375366
cause: cause.clone(),
376-
values: ValuePairs::Aliases(ExpectedFound::new(true, a.into(), b.into())),
367+
values: ValuePairs::Aliases(ExpectedFound::new(a.into(), b.into())),
377368
}
378369
}
379370
}
380371

381372
impl<'tcx> ToTrace<'tcx> for ty::AliasTerm<'tcx> {
382373
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
383-
TypeTrace {
384-
cause: cause.clone(),
385-
values: ValuePairs::Aliases(ExpectedFound::new(true, a, b)),
386-
}
374+
TypeTrace { cause: cause.clone(), values: ValuePairs::Aliases(ExpectedFound::new(a, b)) }
387375
}
388376
}
389377

@@ -392,7 +380,6 @@ impl<'tcx> ToTrace<'tcx> for ty::FnSig<'tcx> {
392380
TypeTrace {
393381
cause: cause.clone(),
394382
values: ValuePairs::PolySigs(ExpectedFound::new(
395-
true,
396383
ty::Binder::dummy(a),
397384
ty::Binder::dummy(b),
398385
)),
@@ -402,18 +389,15 @@ impl<'tcx> ToTrace<'tcx> for ty::FnSig<'tcx> {
402389

403390
impl<'tcx> ToTrace<'tcx> for ty::PolyFnSig<'tcx> {
404391
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
405-
TypeTrace {
406-
cause: cause.clone(),
407-
values: ValuePairs::PolySigs(ExpectedFound::new(true, a, b)),
408-
}
392+
TypeTrace { cause: cause.clone(), values: ValuePairs::PolySigs(ExpectedFound::new(a, b)) }
409393
}
410394
}
411395

412396
impl<'tcx> ToTrace<'tcx> for ty::PolyExistentialTraitRef<'tcx> {
413397
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
414398
TypeTrace {
415399
cause: cause.clone(),
416-
values: ValuePairs::ExistentialTraitRef(ExpectedFound::new(true, a, b)),
400+
values: ValuePairs::ExistentialTraitRef(ExpectedFound::new(a, b)),
417401
}
418402
}
419403
}
@@ -422,7 +406,7 @@ impl<'tcx> ToTrace<'tcx> for ty::PolyExistentialProjection<'tcx> {
422406
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
423407
TypeTrace {
424408
cause: cause.clone(),
425-
values: ValuePairs::ExistentialProjection(ExpectedFound::new(true, a, b)),
409+
values: ValuePairs::ExistentialProjection(ExpectedFound::new(a, b)),
426410
}
427411
}
428412
}

compiler/rustc_infer/src/infer/mod.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -1478,39 +1478,29 @@ impl<'tcx> TypeTrace<'tcx> {
14781478
self.cause.span
14791479
}
14801480

1481-
pub fn types(
1482-
cause: &ObligationCause<'tcx>,
1483-
a_is_expected: bool,
1484-
a: Ty<'tcx>,
1485-
b: Ty<'tcx>,
1486-
) -> TypeTrace<'tcx> {
1481+
pub fn types(cause: &ObligationCause<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> TypeTrace<'tcx> {
14871482
TypeTrace {
14881483
cause: cause.clone(),
1489-
values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
1484+
values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
14901485
}
14911486
}
14921487

14931488
pub fn trait_refs(
14941489
cause: &ObligationCause<'tcx>,
1495-
a_is_expected: bool,
14961490
a: ty::TraitRef<'tcx>,
14971491
b: ty::TraitRef<'tcx>,
14981492
) -> TypeTrace<'tcx> {
1499-
TypeTrace {
1500-
cause: cause.clone(),
1501-
values: ValuePairs::TraitRefs(ExpectedFound::new(a_is_expected, a, b)),
1502-
}
1493+
TypeTrace { cause: cause.clone(), values: ValuePairs::TraitRefs(ExpectedFound::new(a, b)) }
15031494
}
15041495

15051496
pub fn consts(
15061497
cause: &ObligationCause<'tcx>,
1507-
a_is_expected: bool,
15081498
a: ty::Const<'tcx>,
15091499
b: ty::Const<'tcx>,
15101500
) -> TypeTrace<'tcx> {
15111501
TypeTrace {
15121502
cause: cause.clone(),
1513-
values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
1503+
values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
15141504
}
15151505
}
15161506
}

compiler/rustc_infer/src/infer/opaque_types/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<'tcx> InferCtxt<'tcx> {
177177
res
178178
} else {
179179
let (a, b) = self.resolve_vars_if_possible((a, b));
180-
Err(TypeError::Sorts(ExpectedFound::new(true, a, b)))
180+
Err(TypeError::Sorts(ExpectedFound::new(a, b)))
181181
}
182182
}
183183

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,25 @@ extern "C" bool LLVMRustLLVMHasZstdCompressionForDebugSymbols() {
20512051
return llvm::compression::zstd::isAvailable();
20522052
}
20532053

2054+
extern "C" void LLVMRustSetNoSanitizeAddress(LLVMValueRef Global) {
2055+
GlobalValue &GV = *unwrap<GlobalValue>(Global);
2056+
GlobalValue::SanitizerMetadata MD;
2057+
if (GV.hasSanitizerMetadata())
2058+
MD = GV.getSanitizerMetadata();
2059+
MD.NoAddress = true;
2060+
MD.IsDynInit = false;
2061+
GV.setSanitizerMetadata(MD);
2062+
}
2063+
2064+
extern "C" void LLVMRustSetNoSanitizeHWAddress(LLVMValueRef Global) {
2065+
GlobalValue &GV = *unwrap<GlobalValue>(Global);
2066+
GlobalValue::SanitizerMetadata MD;
2067+
if (GV.hasSanitizerMetadata())
2068+
MD = GV.getSanitizerMetadata();
2069+
MD.NoHWAddress = true;
2070+
GV.setSanitizerMetadata(MD);
2071+
}
2072+
20542073
// Operations on composite constants.
20552074
// These are clones of LLVM api functions that will become available in future
20562075
// releases. They can be removed once Rust's minimum supported LLVM version

compiler/rustc_middle/src/ty/relate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<'tcx> Relate<TyCtxt<'tcx>> for &'tcx ty::List<ty::PolyExistentialPredicate<
9292
b_v.sort_by(|a, b| a.skip_binder().stable_cmp(tcx, &b.skip_binder()));
9393
b_v.dedup();
9494
if a_v.len() != b_v.len() {
95-
return Err(TypeError::ExistentialMismatch(ExpectedFound::new(true, a, b)));
95+
return Err(TypeError::ExistentialMismatch(ExpectedFound::new(a, b)));
9696
}
9797

9898
let v = iter::zip(a_v, b_v).map(|(ep_a, ep_b)| {
@@ -112,7 +112,7 @@ impl<'tcx> Relate<TyCtxt<'tcx>> for &'tcx ty::List<ty::PolyExistentialPredicate<
112112
ty::ExistentialPredicate::AutoTrait(a),
113113
ty::ExistentialPredicate::AutoTrait(b),
114114
) if a == b => Ok(ep_a.rebind(ty::ExistentialPredicate::AutoTrait(a))),
115-
_ => Err(TypeError::ExistentialMismatch(ExpectedFound::new(true, a, b))),
115+
_ => Err(TypeError::ExistentialMismatch(ExpectedFound::new(a, b))),
116116
}
117117
});
118118
tcx.mk_poly_existential_predicates_from_iter(v)

compiler/rustc_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,10 @@ passes_no_mangle_foreign =
558558
passes_no_patterns =
559559
patterns not allowed in naked function parameters
560560
561+
passes_no_sanitize =
562+
`#[no_sanitize({$attr_str})]` should be applied to {$accepted_kind}
563+
.label = not {$accepted_kind}
564+
561565
passes_non_exported_macro_invalid_attrs =
562566
attribute should be applied to function or closure
563567
.label = not a function or closure

compiler/rustc_passes/src/check_attr.rs

+34-3
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
126126
[sym::inline, ..] => self.check_inline(hir_id, attr, span, target),
127127
[sym::coverage, ..] => self.check_coverage(attr, span, target),
128128
[sym::optimize, ..] => self.check_optimize(hir_id, attr, span, target),
129-
[sym::no_sanitize, ..] => {
130-
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
131-
}
129+
[sym::no_sanitize, ..] => self.check_no_sanitize(attr, span, target),
132130
[sym::non_exhaustive, ..] => self.check_non_exhaustive(hir_id, attr, span, target),
133131
[sym::marker, ..] => self.check_marker(hir_id, attr, span, target),
134132
[sym::target_feature, ..] => {
@@ -450,6 +448,39 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
450448
}
451449
}
452450

451+
fn check_no_sanitize(&self, attr: &Attribute, span: Span, target: Target) {
452+
if let Some(list) = attr.meta_item_list() {
453+
for item in list.iter() {
454+
let sym = item.name_or_empty();
455+
match sym {
456+
sym::address | sym::hwaddress => {
457+
let is_valid =
458+
matches!(target, Target::Fn | Target::Method(..) | Target::Static);
459+
if !is_valid {
460+
self.dcx().emit_err(errors::NoSanitize {
461+
attr_span: item.span(),
462+
defn_span: span,
463+
accepted_kind: "a function or static",
464+
attr_str: sym.as_str(),
465+
});
466+
}
467+
}
468+
_ => {
469+
let is_valid = matches!(target, Target::Fn | Target::Method(..));
470+
if !is_valid {
471+
self.dcx().emit_err(errors::NoSanitize {
472+
attr_span: item.span(),
473+
defn_span: span,
474+
accepted_kind: "a function",
475+
attr_str: sym.as_str(),
476+
});
477+
}
478+
}
479+
}
480+
}
481+
}
482+
}
483+
453484
fn check_generic_attr(
454485
&self,
455486
hir_id: HirId,

compiler/rustc_passes/src/errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1846,3 +1846,14 @@ pub(crate) struct AttrCrateLevelOnlySugg {
18461846
#[primary_span]
18471847
pub attr: Span,
18481848
}
1849+
1850+
#[derive(Diagnostic)]
1851+
#[diag(passes_no_sanitize)]
1852+
pub(crate) struct NoSanitize<'a> {
1853+
#[primary_span]
1854+
pub attr_span: Span,
1855+
#[label]
1856+
pub defn_span: Span,
1857+
pub accepted_kind: &'a str,
1858+
pub attr_str: &'a str,
1859+
}

compiler/rustc_resolve/messages.ftl

+7-1
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,14 @@ resolve_lowercase_self =
257257
attempt to use a non-constant value in a constant
258258
.suggestion = try using `Self`
259259
260+
resolve_macro_cannot_use_as_attr =
261+
`{$ident}` exists, but a declarative macro cannot be used as an attribute macro
262+
263+
resolve_macro_cannot_use_as_derive =
264+
`{$ident}` exists, but a declarative macro cannot be used as a derive macro
265+
260266
resolve_macro_defined_later =
261-
a macro with the same name exists, but it appears later at here
267+
a macro with the same name exists, but it appears later
262268
263269
resolve_macro_expanded_extern_crate_cannot_shadow_extern_arguments =
264270
macro-expanded `extern crate` items cannot shadow names passed with `--extern`

compiler/rustc_resolve/src/diagnostics.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ use tracing::debug;
3535

3636
use crate::errors::{
3737
self, AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
38-
ExplicitUnsafeTraits, MacroDefinedLater, MacroSuggMovePosition, MaybeMissingMacroRulesName,
38+
ExplicitUnsafeTraits, MacroDefinedLater, MacroRulesNot, MacroSuggMovePosition,
39+
MaybeMissingMacroRulesName,
3940
};
4041
use crate::imports::{Import, ImportKind};
4142
use crate::late::{PatternSource, Rib};
@@ -1473,8 +1474,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14731474
let scope = self.local_macro_def_scopes[&def_id];
14741475
let parent_nearest = parent_scope.module.nearest_parent_mod();
14751476
if Some(parent_nearest) == scope.opt_def_id() {
1476-
err.subdiagnostic(MacroDefinedLater { span: unused_ident.span });
1477-
err.subdiagnostic(MacroSuggMovePosition { span: ident.span, ident });
1477+
match macro_kind {
1478+
MacroKind::Bang => {
1479+
err.subdiagnostic(MacroDefinedLater { span: unused_ident.span });
1480+
err.subdiagnostic(MacroSuggMovePosition { span: ident.span, ident });
1481+
}
1482+
MacroKind::Attr => {
1483+
err.subdiagnostic(MacroRulesNot::Attr { span: unused_ident.span, ident });
1484+
}
1485+
MacroKind::Derive => {
1486+
err.subdiagnostic(MacroRulesNot::Derive { span: unused_ident.span, ident });
1487+
}
1488+
}
1489+
14781490
return;
14791491
}
14801492
}

0 commit comments

Comments
 (0)