Skip to content

Commit 34ccd04

Browse files
committed
Auto merge of #114451 - matthiaskrgr:rollup-37qmv19, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #114022 (Perform OpaqueCast field projection on HIR, too.) - #114253 (Compute variances for lazy type aliases) - #114355 (resolve before canonicalization in new solver, ICE if unresolved) - #114427 (Handle non-utf8 rpaths (fix FIXME)) - #114440 (bootstrap: config: fix version comparison bug) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 73dc6f0 + 50f47d9 commit 34ccd04

File tree

40 files changed

+492
-286
lines changed

40 files changed

+492
-286
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -498,14 +498,13 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
498498

499499
/// Checks that the types internal to the `place` match up with
500500
/// what would be expected.
501+
#[instrument(level = "debug", skip(self, location), ret)]
501502
fn sanitize_place(
502503
&mut self,
503504
place: &Place<'tcx>,
504505
location: Location,
505506
context: PlaceContext,
506507
) -> PlaceTy<'tcx> {
507-
debug!("sanitize_place: {:?}", place);
508-
509508
let mut place_ty = PlaceTy::from_ty(self.body().local_decls[place.local].ty);
510509

511510
for elem in place.projection.iter() {
@@ -608,7 +607,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
608607
}
609608
}
610609

611-
#[instrument(skip(self), level = "debug")]
610+
#[instrument(skip(self, location), ret, level = "debug")]
612611
fn sanitize_projection(
613612
&mut self,
614613
base: PlaceTy<'tcx>,
@@ -617,7 +616,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
617616
location: Location,
618617
context: PlaceContext,
619618
) -> PlaceTy<'tcx> {
620-
debug!("sanitize_projection: {:?} {:?} {:?}", base, pi, place);
621619
let tcx = self.tcx();
622620
let base_ty = base.ty;
623621
match pi {

compiler/rustc_codegen_ssa/src/back/rpath.rs

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use pathdiff::diff_paths;
22
use rustc_data_structures::fx::FxHashSet;
33
use std::env;
4+
use std::ffi::OsString;
45
use std::fs;
56
use std::path::{Path, PathBuf};
67

@@ -12,7 +13,7 @@ pub struct RPathConfig<'a> {
1213
pub linker_is_gnu: bool,
1314
}
1415

15-
pub fn get_rpath_flags(config: &mut RPathConfig<'_>) -> Vec<String> {
16+
pub fn get_rpath_flags(config: &mut RPathConfig<'_>) -> Vec<OsString> {
1617
// No rpath on windows
1718
if !config.has_rpath {
1819
return Vec::new();
@@ -21,36 +22,38 @@ pub fn get_rpath_flags(config: &mut RPathConfig<'_>) -> Vec<String> {
2122
debug!("preparing the RPATH!");
2223

2324
let rpaths = get_rpaths(config);
24-
let mut flags = rpaths_to_flags(&rpaths);
25+
let mut flags = rpaths_to_flags(rpaths);
2526

2627
if config.linker_is_gnu {
2728
// Use DT_RUNPATH instead of DT_RPATH if available
28-
flags.push("-Wl,--enable-new-dtags".to_owned());
29+
flags.push("-Wl,--enable-new-dtags".into());
2930

3031
// Set DF_ORIGIN for substitute $ORIGIN
31-
flags.push("-Wl,-z,origin".to_owned());
32+
flags.push("-Wl,-z,origin".into());
3233
}
3334

3435
flags
3536
}
3637

37-
fn rpaths_to_flags(rpaths: &[String]) -> Vec<String> {
38+
fn rpaths_to_flags(rpaths: Vec<OsString>) -> Vec<OsString> {
3839
let mut ret = Vec::with_capacity(rpaths.len()); // the minimum needed capacity
3940

4041
for rpath in rpaths {
41-
if rpath.contains(',') {
42+
if rpath.to_string_lossy().contains(',') {
4243
ret.push("-Wl,-rpath".into());
4344
ret.push("-Xlinker".into());
44-
ret.push(rpath.clone());
45+
ret.push(rpath);
4546
} else {
46-
ret.push(format!("-Wl,-rpath,{}", &(*rpath)));
47+
let mut single_arg = OsString::from("-Wl,-rpath,");
48+
single_arg.push(rpath);
49+
ret.push(single_arg);
4750
}
4851
}
4952

5053
ret
5154
}
5255

53-
fn get_rpaths(config: &mut RPathConfig<'_>) -> Vec<String> {
56+
fn get_rpaths(config: &mut RPathConfig<'_>) -> Vec<OsString> {
5457
debug!("output: {:?}", config.out_filename.display());
5558
debug!("libs:");
5659
for libpath in config.libs {
@@ -64,18 +67,18 @@ fn get_rpaths(config: &mut RPathConfig<'_>) -> Vec<String> {
6467

6568
debug!("rpaths:");
6669
for rpath in &rpaths {
67-
debug!(" {}", rpath);
70+
debug!(" {:?}", rpath);
6871
}
6972

7073
// Remove duplicates
7174
minimize_rpaths(&rpaths)
7275
}
7376

74-
fn get_rpaths_relative_to_output(config: &mut RPathConfig<'_>) -> Vec<String> {
77+
fn get_rpaths_relative_to_output(config: &mut RPathConfig<'_>) -> Vec<OsString> {
7578
config.libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect()
7679
}
7780

78-
fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> String {
81+
fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> OsString {
7982
// Mac doesn't appear to support $ORIGIN
8083
let prefix = if config.is_like_osx { "@loader_path" } else { "$ORIGIN" };
8184

@@ -87,8 +90,11 @@ fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> Str
8790
let output = fs::canonicalize(&output).unwrap_or(output);
8891
let relative = path_relative_from(&lib, &output)
8992
.unwrap_or_else(|| panic!("couldn't create relative path from {output:?} to {lib:?}"));
90-
// FIXME (#9639): This needs to handle non-utf8 paths
91-
format!("{}/{}", prefix, relative.to_str().expect("non-utf8 component in path"))
93+
94+
let mut rpath = OsString::from(prefix);
95+
rpath.push("/");
96+
rpath.push(relative);
97+
rpath
9298
}
9399

94100
// This routine is adapted from the *old* Path's `path_relative_from`
@@ -99,7 +105,7 @@ fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> {
99105
diff_paths(path, base)
100106
}
101107

102-
fn minimize_rpaths(rpaths: &[String]) -> Vec<String> {
108+
fn minimize_rpaths(rpaths: &[OsString]) -> Vec<OsString> {
103109
let mut set = FxHashSet::default();
104110
let mut minimized = Vec::new();
105111
for rpath in rpaths {

compiler/rustc_codegen_ssa/src/back/rpath/tests.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
use super::RPathConfig;
22
use super::{get_rpath_relative_to_output, minimize_rpaths, rpaths_to_flags};
3+
use std::ffi::OsString;
34
use std::path::{Path, PathBuf};
45

56
#[test]
67
fn test_rpaths_to_flags() {
7-
let flags = rpaths_to_flags(&["path1".to_string(), "path2".to_string()]);
8+
let flags = rpaths_to_flags(vec!["path1".into(), "path2".into()]);
89
assert_eq!(flags, ["-Wl,-rpath,path1", "-Wl,-rpath,path2"]);
910
}
1011

1112
#[test]
1213
fn test_minimize1() {
13-
let res = minimize_rpaths(&["rpath1".to_string(), "rpath2".to_string(), "rpath1".to_string()]);
14+
let res = minimize_rpaths(&["rpath1".into(), "rpath2".into(), "rpath1".into()]);
1415
assert!(res == ["rpath1", "rpath2",]);
1516
}
1617

1718
#[test]
1819
fn test_minimize2() {
1920
let res = minimize_rpaths(&[
20-
"1a".to_string(),
21-
"2".to_string(),
22-
"2".to_string(),
23-
"1a".to_string(),
24-
"4a".to_string(),
25-
"1a".to_string(),
26-
"2".to_string(),
27-
"3".to_string(),
28-
"4a".to_string(),
29-
"3".to_string(),
21+
"1a".into(),
22+
"2".into(),
23+
"2".into(),
24+
"1a".into(),
25+
"4a".into(),
26+
"1a".into(),
27+
"2".into(),
28+
"3".into(),
29+
"4a".into(),
30+
"3".into(),
3031
]);
3132
assert!(res == ["1a", "2", "4a", "3",]);
3233
}
@@ -58,15 +59,15 @@ fn test_rpath_relative() {
5859

5960
#[test]
6061
fn test_xlinker() {
61-
let args = rpaths_to_flags(&["a/normal/path".to_string(), "a,comma,path".to_string()]);
62+
let args = rpaths_to_flags(vec!["a/normal/path".into(), "a,comma,path".into()]);
6263

6364
assert_eq!(
6465
args,
6566
vec![
66-
"-Wl,-rpath,a/normal/path".to_string(),
67-
"-Wl,-rpath".to_string(),
68-
"-Xlinker".to_string(),
69-
"a,comma,path".to_string()
67+
OsString::from("-Wl,-rpath,a/normal/path"),
68+
OsString::from("-Wl,-rpath"),
69+
OsString::from("-Xlinker"),
70+
OsString::from("a,comma,path")
7071
]
7172
);
7273
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,14 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {
245245
}
246246
// `ForeignItem`s are handled separately.
247247
hir::ItemKind::ForeignMod { .. } => {}
248-
hir::ItemKind::TyAlias(hir_ty, ..) => {
248+
hir::ItemKind::TyAlias(hir_ty, ast_generics) => {
249249
if tcx.features().lazy_type_alias
250250
|| tcx.type_of(item.owner_id).skip_binder().has_opaque_types()
251251
{
252252
// Bounds of lazy type aliases and of eager ones that contain opaque types are respected.
253253
// E.g: `type X = impl Trait;`, `type X = (impl Trait, Y);`.
254254
check_item_type(tcx, def_id, hir_ty.span, UnsizedHandling::Allow);
255+
check_variances_for_type_defn(tcx, item, ast_generics);
255256
}
256257
}
257258
_ => {}
@@ -1700,10 +1701,27 @@ fn check_variances_for_type_defn<'tcx>(
17001701
hir_generics: &hir::Generics<'_>,
17011702
) {
17021703
let identity_args = ty::GenericArgs::identity_for_item(tcx, item.owner_id);
1703-
for field in tcx.adt_def(item.owner_id).all_fields() {
1704-
if field.ty(tcx, identity_args).references_error() {
1705-
return;
1704+
1705+
match item.kind {
1706+
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {
1707+
for field in tcx.adt_def(item.owner_id).all_fields() {
1708+
if field.ty(tcx, identity_args).references_error() {
1709+
return;
1710+
}
1711+
}
1712+
}
1713+
ItemKind::TyAlias(..) => {
1714+
let ty = tcx.type_of(item.owner_id).instantiate_identity();
1715+
1716+
if tcx.features().lazy_type_alias || ty.has_opaque_types() {
1717+
if ty.references_error() {
1718+
return;
1719+
}
1720+
} else {
1721+
bug!();
1722+
}
17061723
}
1724+
_ => bug!(),
17071725
}
17081726

17091727
let ty_predicates = tcx.predicates_of(item.owner_id);

compiler/rustc_hir_analysis/src/variance/constraints.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use hir::def_id::{DefId, LocalDefId};
77
use rustc_hir as hir;
88
use rustc_hir::def::DefKind;
9-
use rustc_middle::ty::{self, Ty, TyCtxt};
9+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
1010
use rustc_middle::ty::{GenericArgKind, GenericArgsRef};
1111

1212
use super::terms::VarianceTerm::*;
@@ -78,6 +78,12 @@ pub fn add_constraints_from_crate<'a, 'tcx>(
7878
}
7979
}
8080
DefKind::Fn | DefKind::AssocFn => constraint_cx.build_constraints_for_item(def_id),
81+
DefKind::TyAlias
82+
if tcx.features().lazy_type_alias
83+
|| tcx.type_of(def_id).instantiate_identity().has_opaque_types() =>
84+
{
85+
constraint_cx.build_constraints_for_item(def_id)
86+
}
8187
_ => {}
8288
}
8389
}
@@ -101,7 +107,18 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
101107

102108
let inferred_start = self.terms_cx.inferred_starts[&def_id];
103109
let current_item = &CurrentItem { inferred_start };
104-
match tcx.type_of(def_id).instantiate_identity().kind() {
110+
let ty = tcx.type_of(def_id).instantiate_identity();
111+
112+
// The type as returned by `type_of` is the underlying type and generally not a weak projection.
113+
// Therefore we need to check the `DefKind` first.
114+
if let DefKind::TyAlias = tcx.def_kind(def_id)
115+
&& (tcx.features().lazy_type_alias || ty.has_opaque_types())
116+
{
117+
self.add_constraints_from_ty(current_item, ty, self.covariant);
118+
return;
119+
}
120+
121+
match ty.kind() {
105122
ty::Adt(def, _) => {
106123
// Not entirely obvious: constraints on structs/enums do not
107124
// affect the variance of their type parameters. See discussion
@@ -127,6 +144,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
127144
}
128145

129146
ty::Error(_) => {}
147+
130148
_ => {
131149
span_bug!(
132150
tcx.def_span(def_id),
@@ -252,10 +270,14 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
252270
self.add_constraints_from_args(current, def.did(), args, variance);
253271
}
254272

255-
ty::Alias(_, ref data) => {
273+
ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, ref data) => {
256274
self.add_constraints_from_invariant_args(current, data.args, variance);
257275
}
258276

277+
ty::Alias(ty::Weak, ref data) => {
278+
self.add_constraints_from_args(current, data.def_id, data.args, variance);
279+
}
280+
259281
ty::Dynamic(data, r, _) => {
260282
// The type `dyn Trait<T> +'a` is covariant w/r/t `'a`:
261283
self.add_constraints_from_region(current, r, variance);

compiler/rustc_hir_analysis/src/variance/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::def::DefKind;
88
use rustc_hir::def_id::{DefId, LocalDefId};
99
use rustc_middle::query::Providers;
1010
use rustc_middle::ty::{self, CrateVariancesMap, GenericArgsRef, Ty, TyCtxt};
11-
use rustc_middle::ty::{TypeSuperVisitable, TypeVisitable};
11+
use rustc_middle::ty::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt};
1212
use std::ops::ControlFlow;
1313

1414
/// Defines the `TermsContext` basically houses an arena where we can
@@ -56,6 +56,14 @@ fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] {
5656
let crate_map = tcx.crate_variances(());
5757
return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]);
5858
}
59+
DefKind::TyAlias
60+
if tcx.features().lazy_type_alias
61+
|| tcx.type_of(item_def_id).instantiate_identity().has_opaque_types() =>
62+
{
63+
// These are inferred.
64+
let crate_map = tcx.crate_variances(());
65+
return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]);
66+
}
5967
DefKind::OpaqueTy => {
6068
return variance_of_opaque(tcx, item_def_id);
6169
}

compiler/rustc_hir_analysis/src/variance/terms.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use rustc_arena::DroplessArena;
1313
use rustc_hir::def::DefKind;
1414
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap};
15-
use rustc_middle::ty::{self, TyCtxt};
15+
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
1616
use std::fmt;
1717

1818
use self::VarianceTerm::*;
@@ -97,6 +97,12 @@ pub fn determine_parameters_to_be_inferred<'a, 'tcx>(
9797
}
9898
}
9999
DefKind::Fn | DefKind::AssocFn => terms_cx.add_inferreds_for_item(def_id),
100+
DefKind::TyAlias
101+
if tcx.features().lazy_type_alias
102+
|| tcx.type_of(def_id).instantiate_identity().has_opaque_types() =>
103+
{
104+
terms_cx.add_inferreds_for_item(def_id)
105+
}
100106
_ => {}
101107
}
102108
}

0 commit comments

Comments
 (0)