Skip to content

Commit de824fa

Browse files
committed
Auto merge of #129502 - matthiaskrgr:rollup-wtigxhj, r=matthiaskrgr
Rollup of 11 pull requests Successful merges: - #127021 (Add target support for RTEMS Arm) - #128467 (Detect `*` operator on `!Sized` expression) - #128524 (Don't suggest turning crate-level attributes into outer style) - #128735 (Add a special case for `CStr`/`CString` in the `improper_ctypes` lint) - #129418 (rustc: Simplify getting sysroot library directory) - #129429 (Print the generic parameter along with the variance in dumps.) - #129430 (rustdoc: show exact case-sensitive matches first) - #129449 (Put Pin::as_deref_mut in impl Pin<Ptr> / rearrange Pin methods) - #129481 (Update `compiler_builtins` to `0.1.121`) - #129482 (Add myself to the review rotation for libs) - #129492 (make text more easy to read) r? `@ghost` `@rustbot` modify labels: rollup
2 parents edbc000 + 3cc95cb commit de824fa

File tree

100 files changed

+1279
-360
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1279
-360
lines changed

compiler/rustc_ast/src/ast.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2902,6 +2902,17 @@ pub struct AttrItem {
29022902
pub tokens: Option<LazyAttrTokenStream>,
29032903
}
29042904

2905+
impl AttrItem {
2906+
pub fn is_valid_for_outer_style(&self) -> bool {
2907+
self.path == sym::cfg_attr
2908+
|| self.path == sym::cfg
2909+
|| self.path == sym::forbid
2910+
|| self.path == sym::warn
2911+
|| self.path == sym::allow
2912+
|| self.path == sym::deny
2913+
}
2914+
}
2915+
29052916
/// `TraitRef`s appear in impls.
29062917
///
29072918
/// Resolution maps each `TraitRef`'s `ref_id` to its defining trait; that's all

compiler/rustc_codegen_ssa/src/back/link.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -1317,11 +1317,9 @@ fn link_sanitizer_runtime(
13171317
name: &str,
13181318
) {
13191319
fn find_sanitizer_runtime(sess: &Session, filename: &str) -> PathBuf {
1320-
let session_tlib =
1321-
filesearch::make_target_lib_path(&sess.sysroot, sess.opts.target_triple.triple());
1322-
let path = session_tlib.join(filename);
1320+
let path = sess.target_tlib_path.dir.join(filename);
13231321
if path.exists() {
1324-
return session_tlib;
1322+
return sess.target_tlib_path.dir.clone();
13251323
} else {
13261324
let default_sysroot =
13271325
filesearch::get_or_default_sysroot().expect("Failed finding sysroot");
@@ -1612,19 +1610,18 @@ fn print_native_static_libs(
16121610
}
16131611

16141612
fn get_object_file_path(sess: &Session, name: &str, self_contained: bool) -> PathBuf {
1615-
let fs = sess.target_filesearch(PathKind::Native);
1616-
let file_path = fs.get_lib_path().join(name);
1613+
let file_path = sess.target_tlib_path.dir.join(name);
16171614
if file_path.exists() {
16181615
return file_path;
16191616
}
16201617
// Special directory with objects used only in self-contained linkage mode
16211618
if self_contained {
1622-
let file_path = fs.get_self_contained_lib_path().join(name);
1619+
let file_path = sess.target_tlib_path.dir.join("self-contained").join(name);
16231620
if file_path.exists() {
16241621
return file_path;
16251622
}
16261623
}
1627-
for search_path in fs.search_paths() {
1624+
for search_path in sess.target_filesearch(PathKind::Native).search_paths() {
16281625
let file_path = search_path.dir.join(name);
16291626
if file_path.exists() {
16301627
return file_path;
@@ -2131,7 +2128,7 @@ fn add_library_search_dirs(
21312128
| LinkSelfContainedComponents::UNWIND
21322129
| LinkSelfContainedComponents::MINGW,
21332130
) {
2134-
let lib_path = sess.target_filesearch(PathKind::Native).get_self_contained_lib_path();
2131+
let lib_path = sess.target_tlib_path.dir.join("self-contained");
21352132
cmd.include_path(&fix_windows_verbatim_for_gcc(&lib_path));
21362133
}
21372134

@@ -2146,8 +2143,7 @@ fn add_library_search_dirs(
21462143
|| sess.target.os == "fuchsia"
21472144
|| sess.target.is_like_osx && !sess.opts.unstable_opts.sanitizer.is_empty()
21482145
{
2149-
let lib_path = sess.target_filesearch(PathKind::Native).get_lib_path();
2150-
cmd.include_path(&fix_windows_verbatim_for_gcc(&lib_path));
2146+
cmd.include_path(&fix_windows_verbatim_for_gcc(&sess.target_tlib_path.dir));
21512147
}
21522148

21532149
// Mac Catalyst uses the macOS SDK, but to link to iOS-specific frameworks
@@ -2859,15 +2855,14 @@ fn add_upstream_native_libraries(
28592855
//
28602856
// The returned path will always have `fix_windows_verbatim_for_gcc()` applied to it.
28612857
fn rehome_sysroot_lib_dir(sess: &Session, lib_dir: &Path) -> PathBuf {
2862-
let sysroot_lib_path = sess.target_filesearch(PathKind::All).get_lib_path();
2858+
let sysroot_lib_path = &sess.target_tlib_path.dir;
28632859
let canonical_sysroot_lib_path =
2864-
{ try_canonicalize(&sysroot_lib_path).unwrap_or_else(|_| sysroot_lib_path.clone()) };
2860+
{ try_canonicalize(sysroot_lib_path).unwrap_or_else(|_| sysroot_lib_path.clone()) };
28652861

28662862
let canonical_lib_dir = try_canonicalize(lib_dir).unwrap_or_else(|_| lib_dir.to_path_buf());
28672863
if canonical_lib_dir == canonical_sysroot_lib_path {
2868-
// This path, returned by `target_filesearch().get_lib_path()`, has
2869-
// already had `fix_windows_verbatim_for_gcc()` applied if needed.
2870-
sysroot_lib_path
2864+
// This path already had `fix_windows_verbatim_for_gcc()` applied if needed.
2865+
sysroot_lib_path.clone()
28712866
} else {
28722867
fix_windows_verbatim_for_gcc(lib_dir)
28732868
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1+
use std::fmt::Write;
2+
13
use rustc_hir::def::DefKind;
2-
use rustc_hir::def_id::CRATE_DEF_ID;
3-
use rustc_middle::ty::TyCtxt;
4+
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
5+
use rustc_middle::ty::{GenericArgs, TyCtxt};
46
use rustc_span::symbol::sym;
57

8+
fn format_variances(tcx: TyCtxt<'_>, def_id: LocalDefId) -> String {
9+
let variances = tcx.variances_of(def_id);
10+
let generics = GenericArgs::identity_for_item(tcx, def_id);
11+
// 7 = 2-letter parameter + ": " + 1-letter variance + ", "
12+
let mut ret = String::with_capacity(2 + 7 * variances.len());
13+
ret.push('[');
14+
for (arg, variance) in generics.iter().zip(variances.iter()) {
15+
write!(ret, "{arg}: {variance:?}, ").unwrap();
16+
}
17+
// Remove trailing `, `.
18+
if !variances.is_empty() {
19+
ret.pop();
20+
ret.pop();
21+
}
22+
ret.push(']');
23+
ret
24+
}
25+
626
pub(crate) fn variances(tcx: TyCtxt<'_>) {
727
if tcx.has_attr(CRATE_DEF_ID, sym::rustc_variance_of_opaques) {
828
for id in tcx.hir().items() {
929
let DefKind::OpaqueTy = tcx.def_kind(id.owner_id) else { continue };
1030

11-
let variances = tcx.variances_of(id.owner_id);
12-
1331
tcx.dcx().emit_err(crate::errors::VariancesOf {
1432
span: tcx.def_span(id.owner_id),
15-
variances: format!("{variances:?}"),
33+
variances: format_variances(tcx, id.owner_id.def_id),
1634
});
1735
}
1836
}
@@ -22,11 +40,9 @@ pub(crate) fn variances(tcx: TyCtxt<'_>) {
2240
continue;
2341
}
2442

25-
let variances = tcx.variances_of(id.owner_id);
26-
2743
tcx.dcx().emit_err(crate::errors::VariancesOf {
2844
span: tcx.def_span(id.owner_id),
29-
variances: format!("{variances:?}"),
45+
variances: format_variances(tcx, id.owner_id.def_id),
3046
});
3147
}
3248
}

compiler/rustc_hir_typeck/src/expr.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -841,13 +841,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
841841
let ret_ty = ret_coercion.borrow().expected_ty();
842842
let return_expr_ty = self.check_expr_with_hint(return_expr, ret_ty);
843843
let mut span = return_expr.span;
844+
let mut hir_id = return_expr.hir_id;
844845
// Use the span of the trailing expression for our cause,
845846
// not the span of the entire function
846847
if !explicit_return
847848
&& let ExprKind::Block(body, _) = return_expr.kind
848849
&& let Some(last_expr) = body.expr
849850
{
850851
span = last_expr.span;
852+
hir_id = last_expr.hir_id;
851853
}
852854
ret_coercion.borrow_mut().coerce(
853855
self,
@@ -864,6 +866,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
864866
self.select_obligations_where_possible(|errors| {
865867
self.point_at_return_for_opaque_ty_error(
866868
errors,
869+
hir_id,
867870
span,
868871
return_expr_ty,
869872
return_expr.span,
@@ -921,6 +924,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
921924
fn point_at_return_for_opaque_ty_error(
922925
&self,
923926
errors: &mut Vec<traits::FulfillmentError<'tcx>>,
927+
hir_id: HirId,
924928
span: Span,
925929
return_expr_ty: Ty<'tcx>,
926930
return_span: Span,
@@ -935,7 +939,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
935939
let new_cause = ObligationCause::new(
936940
cause.span,
937941
cause.body_id,
938-
ObligationCauseCode::OpaqueReturnType(Some((return_expr_ty, span))),
942+
ObligationCauseCode::OpaqueReturnType(Some((return_expr_ty, hir_id))),
939943
);
940944
*cause = new_cause;
941945
}

compiler/rustc_lint/messages.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ lint_improper_ctypes_box = box cannot be represented as a single pointer
361361
lint_improper_ctypes_char_help = consider using `u32` or `libc::wchar_t` instead
362362
363363
lint_improper_ctypes_char_reason = the `char` type has no C equivalent
364+
365+
lint_improper_ctypes_cstr_help =
366+
consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()`
367+
lint_improper_ctypes_cstr_reason = `CStr`/`CString` do not have a guaranteed layout
368+
364369
lint_improper_ctypes_dyn = trait objects have no C equivalent
365370
366371
lint_improper_ctypes_enum_repr_help =

compiler/rustc_lint/src/types.rs

+39-15
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,14 @@ struct ImproperCTypesVisitor<'a, 'tcx> {
985985
mode: CItemKind,
986986
}
987987

988+
/// Accumulator for recursive ffi type checking
989+
struct CTypesVisitorState<'tcx> {
990+
cache: FxHashSet<Ty<'tcx>>,
991+
/// The original type being checked, before we recursed
992+
/// to any other types it contains.
993+
base_ty: Ty<'tcx>,
994+
}
995+
988996
enum FfiResult<'tcx> {
989997
FfiSafe,
990998
FfiPhantom(Ty<'tcx>),
@@ -1213,7 +1221,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12131221
/// Checks if the given field's type is "ffi-safe".
12141222
fn check_field_type_for_ffi(
12151223
&self,
1216-
cache: &mut FxHashSet<Ty<'tcx>>,
1224+
acc: &mut CTypesVisitorState<'tcx>,
12171225
field: &ty::FieldDef,
12181226
args: GenericArgsRef<'tcx>,
12191227
) -> FfiResult<'tcx> {
@@ -1223,13 +1231,13 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12231231
.tcx
12241232
.try_normalize_erasing_regions(self.cx.param_env, field_ty)
12251233
.unwrap_or(field_ty);
1226-
self.check_type_for_ffi(cache, field_ty)
1234+
self.check_type_for_ffi(acc, field_ty)
12271235
}
12281236

12291237
/// Checks if the given `VariantDef`'s field types are "ffi-safe".
12301238
fn check_variant_for_ffi(
12311239
&self,
1232-
cache: &mut FxHashSet<Ty<'tcx>>,
1240+
acc: &mut CTypesVisitorState<'tcx>,
12331241
ty: Ty<'tcx>,
12341242
def: ty::AdtDef<'tcx>,
12351243
variant: &ty::VariantDef,
@@ -1239,7 +1247,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12391247
let transparent_with_all_zst_fields = if def.repr().transparent() {
12401248
if let Some(field) = transparent_newtype_field(self.cx.tcx, variant) {
12411249
// Transparent newtypes have at most one non-ZST field which needs to be checked..
1242-
match self.check_field_type_for_ffi(cache, field, args) {
1250+
match self.check_field_type_for_ffi(acc, field, args) {
12431251
FfiUnsafe { ty, .. } if ty.is_unit() => (),
12441252
r => return r,
12451253
}
@@ -1257,7 +1265,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12571265
// We can't completely trust `repr(C)` markings, so make sure the fields are actually safe.
12581266
let mut all_phantom = !variant.fields.is_empty();
12591267
for field in &variant.fields {
1260-
all_phantom &= match self.check_field_type_for_ffi(cache, field, args) {
1268+
all_phantom &= match self.check_field_type_for_ffi(acc, field, args) {
12611269
FfiSafe => false,
12621270
// `()` fields are FFI-safe!
12631271
FfiUnsafe { ty, .. } if ty.is_unit() => false,
@@ -1277,7 +1285,11 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12771285

12781286
/// Checks if the given type is "ffi-safe" (has a stable, well-defined
12791287
/// representation which can be exported to C code).
1280-
fn check_type_for_ffi(&self, cache: &mut FxHashSet<Ty<'tcx>>, ty: Ty<'tcx>) -> FfiResult<'tcx> {
1288+
fn check_type_for_ffi(
1289+
&self,
1290+
acc: &mut CTypesVisitorState<'tcx>,
1291+
ty: Ty<'tcx>,
1292+
) -> FfiResult<'tcx> {
12811293
use FfiResult::*;
12821294

12831295
let tcx = self.cx.tcx;
@@ -1286,7 +1298,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12861298
// `struct S(*mut S);`.
12871299
// FIXME: A recursion limit is necessary as well, for irregular
12881300
// recursive types.
1289-
if !cache.insert(ty) {
1301+
if !acc.cache.insert(ty) {
12901302
return FfiSafe;
12911303
}
12921304

@@ -1308,6 +1320,17 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13081320
}
13091321
match def.adt_kind() {
13101322
AdtKind::Struct | AdtKind::Union => {
1323+
if let Some(sym::cstring_type | sym::cstr_type) =
1324+
tcx.get_diagnostic_name(def.did())
1325+
&& !acc.base_ty.is_mutable_ptr()
1326+
{
1327+
return FfiUnsafe {
1328+
ty,
1329+
reason: fluent::lint_improper_ctypes_cstr_reason,
1330+
help: Some(fluent::lint_improper_ctypes_cstr_help),
1331+
};
1332+
}
1333+
13111334
if !def.repr().c() && !def.repr().transparent() {
13121335
return FfiUnsafe {
13131336
ty,
@@ -1354,7 +1377,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13541377
};
13551378
}
13561379

1357-
self.check_variant_for_ffi(cache, ty, def, def.non_enum_variant(), args)
1380+
self.check_variant_for_ffi(acc, ty, def, def.non_enum_variant(), args)
13581381
}
13591382
AdtKind::Enum => {
13601383
if def.variants().is_empty() {
@@ -1378,7 +1401,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13781401
if let Some(ty) =
13791402
repr_nullable_ptr(self.cx.tcx, self.cx.param_env, ty, self.mode)
13801403
{
1381-
return self.check_type_for_ffi(cache, ty);
1404+
return self.check_type_for_ffi(acc, ty);
13821405
}
13831406

13841407
return FfiUnsafe {
@@ -1399,7 +1422,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13991422
};
14001423
}
14011424

1402-
match self.check_variant_for_ffi(cache, ty, def, variant, args) {
1425+
match self.check_variant_for_ffi(acc, ty, def, variant, args) {
14031426
FfiSafe => (),
14041427
r => return r,
14051428
}
@@ -1469,9 +1492,9 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
14691492
FfiSafe
14701493
}
14711494

1472-
ty::RawPtr(ty, _) | ty::Ref(_, ty, _) => self.check_type_for_ffi(cache, ty),
1495+
ty::RawPtr(ty, _) | ty::Ref(_, ty, _) => self.check_type_for_ffi(acc, ty),
14731496

1474-
ty::Array(inner_ty, _) => self.check_type_for_ffi(cache, inner_ty),
1497+
ty::Array(inner_ty, _) => self.check_type_for_ffi(acc, inner_ty),
14751498

14761499
ty::FnPtr(sig_tys, hdr) => {
14771500
let sig = sig_tys.with(hdr);
@@ -1485,7 +1508,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
14851508

14861509
let sig = tcx.instantiate_bound_regions_with_erased(sig);
14871510
for arg in sig.inputs() {
1488-
match self.check_type_for_ffi(cache, *arg) {
1511+
match self.check_type_for_ffi(acc, *arg) {
14891512
FfiSafe => {}
14901513
r => return r,
14911514
}
@@ -1496,7 +1519,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
14961519
return FfiSafe;
14971520
}
14981521

1499-
self.check_type_for_ffi(cache, ret_ty)
1522+
self.check_type_for_ffi(acc, ret_ty)
15001523
}
15011524

15021525
ty::Foreign(..) => FfiSafe,
@@ -1619,7 +1642,8 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
16191642
return;
16201643
}
16211644

1622-
match self.check_type_for_ffi(&mut FxHashSet::default(), ty) {
1645+
let mut acc = CTypesVisitorState { cache: FxHashSet::default(), base_ty: ty };
1646+
match self.check_type_for_ffi(&mut acc, ty) {
16231647
FfiResult::FfiSafe => {}
16241648
FfiResult::FfiPhantom(ty) => {
16251649
self.emit_ffi_unsafe_type_lint(

compiler/rustc_middle/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ pub enum ObligationCauseCode<'tcx> {
353353
ReturnValue(HirId),
354354

355355
/// Opaque return type of this function
356-
OpaqueReturnType(Option<(Ty<'tcx>, Span)>),
356+
OpaqueReturnType(Option<(Ty<'tcx>, HirId)>),
357357

358358
/// Block implicit return
359359
BlockTailExpression(HirId, hir::MatchSource),

0 commit comments

Comments
 (0)