Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 5 pull requests #114451

Merged
merged 16 commits into from
Aug 4, 2023
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
6 changes: 2 additions & 4 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,13 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {

/// Checks that the types internal to the `place` match up with
/// what would be expected.
#[instrument(level = "debug", skip(self, location), ret)]
fn sanitize_place(
&mut self,
place: &Place<'tcx>,
location: Location,
context: PlaceContext,
) -> PlaceTy<'tcx> {
debug!("sanitize_place: {:?}", place);

let mut place_ty = PlaceTy::from_ty(self.body().local_decls[place.local].ty);

for elem in place.projection.iter() {
Expand Down Expand Up @@ -608,7 +607,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
}
}

#[instrument(skip(self), level = "debug")]
#[instrument(skip(self, location), ret, level = "debug")]
fn sanitize_projection(
&mut self,
base: PlaceTy<'tcx>,
Expand All @@ -617,7 +616,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
location: Location,
context: PlaceContext,
) -> PlaceTy<'tcx> {
debug!("sanitize_projection: {:?} {:?} {:?}", base, pi, place);
let tcx = self.tcx();
let base_ty = base.ty;
match pi {
Expand Down
36 changes: 21 additions & 15 deletions compiler/rustc_codegen_ssa/src/back/rpath.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use pathdiff::diff_paths;
use rustc_data_structures::fx::FxHashSet;
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::{Path, PathBuf};

Expand All @@ -12,7 +13,7 @@ pub struct RPathConfig<'a> {
pub linker_is_gnu: bool,
}

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

let rpaths = get_rpaths(config);
let mut flags = rpaths_to_flags(&rpaths);
let mut flags = rpaths_to_flags(rpaths);

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

// Set DF_ORIGIN for substitute $ORIGIN
flags.push("-Wl,-z,origin".to_owned());
flags.push("-Wl,-z,origin".into());
}

flags
}

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

for rpath in rpaths {
if rpath.contains(',') {
if rpath.to_string_lossy().contains(',') {
ret.push("-Wl,-rpath".into());
ret.push("-Xlinker".into());
ret.push(rpath.clone());
ret.push(rpath);
} else {
ret.push(format!("-Wl,-rpath,{}", &(*rpath)));
let mut single_arg = OsString::from("-Wl,-rpath,");
single_arg.push(rpath);
ret.push(single_arg);
}
}

ret
}

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

debug!("rpaths:");
for rpath in &rpaths {
debug!(" {}", rpath);
debug!(" {:?}", rpath);
}

// Remove duplicates
minimize_rpaths(&rpaths)
}

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

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

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

let mut rpath = OsString::from(prefix);
rpath.push("/");
rpath.push(relative);
rpath
}

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

fn minimize_rpaths(rpaths: &[String]) -> Vec<String> {
fn minimize_rpaths(rpaths: &[OsString]) -> Vec<OsString> {
let mut set = FxHashSet::default();
let mut minimized = Vec::new();
for rpath in rpaths {
Expand Down
35 changes: 18 additions & 17 deletions compiler/rustc_codegen_ssa/src/back/rpath/tests.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
use super::RPathConfig;
use super::{get_rpath_relative_to_output, minimize_rpaths, rpaths_to_flags};
use std::ffi::OsString;
use std::path::{Path, PathBuf};

#[test]
fn test_rpaths_to_flags() {
let flags = rpaths_to_flags(&["path1".to_string(), "path2".to_string()]);
let flags = rpaths_to_flags(vec!["path1".into(), "path2".into()]);
assert_eq!(flags, ["-Wl,-rpath,path1", "-Wl,-rpath,path2"]);
}

#[test]
fn test_minimize1() {
let res = minimize_rpaths(&["rpath1".to_string(), "rpath2".to_string(), "rpath1".to_string()]);
let res = minimize_rpaths(&["rpath1".into(), "rpath2".into(), "rpath1".into()]);
assert!(res == ["rpath1", "rpath2",]);
}

#[test]
fn test_minimize2() {
let res = minimize_rpaths(&[
"1a".to_string(),
"2".to_string(),
"2".to_string(),
"1a".to_string(),
"4a".to_string(),
"1a".to_string(),
"2".to_string(),
"3".to_string(),
"4a".to_string(),
"3".to_string(),
"1a".into(),
"2".into(),
"2".into(),
"1a".into(),
"4a".into(),
"1a".into(),
"2".into(),
"3".into(),
"4a".into(),
"3".into(),
]);
assert!(res == ["1a", "2", "4a", "3",]);
}
Expand Down Expand Up @@ -58,15 +59,15 @@ fn test_rpath_relative() {

#[test]
fn test_xlinker() {
let args = rpaths_to_flags(&["a/normal/path".to_string(), "a,comma,path".to_string()]);
let args = rpaths_to_flags(vec!["a/normal/path".into(), "a,comma,path".into()]);

assert_eq!(
args,
vec![
"-Wl,-rpath,a/normal/path".to_string(),
"-Wl,-rpath".to_string(),
"-Xlinker".to_string(),
"a,comma,path".to_string()
OsString::from("-Wl,-rpath,a/normal/path"),
OsString::from("-Wl,-rpath"),
OsString::from("-Xlinker"),
OsString::from("a,comma,path")
]
);
}
26 changes: 22 additions & 4 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,14 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {
}
// `ForeignItem`s are handled separately.
hir::ItemKind::ForeignMod { .. } => {}
hir::ItemKind::TyAlias(hir_ty, ..) => {
hir::ItemKind::TyAlias(hir_ty, ast_generics) => {
if tcx.features().lazy_type_alias
|| tcx.type_of(item.owner_id).skip_binder().has_opaque_types()
{
// Bounds of lazy type aliases and of eager ones that contain opaque types are respected.
// E.g: `type X = impl Trait;`, `type X = (impl Trait, Y);`.
check_item_type(tcx, def_id, hir_ty.span, UnsizedHandling::Allow);
check_variances_for_type_defn(tcx, item, ast_generics);
}
}
_ => {}
Expand Down Expand Up @@ -1700,10 +1701,27 @@ fn check_variances_for_type_defn<'tcx>(
hir_generics: &hir::Generics<'_>,
) {
let identity_args = ty::GenericArgs::identity_for_item(tcx, item.owner_id);
for field in tcx.adt_def(item.owner_id).all_fields() {
if field.ty(tcx, identity_args).references_error() {
return;

match item.kind {
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {
for field in tcx.adt_def(item.owner_id).all_fields() {
if field.ty(tcx, identity_args).references_error() {
return;
}
}
}
ItemKind::TyAlias(..) => {
let ty = tcx.type_of(item.owner_id).instantiate_identity();

if tcx.features().lazy_type_alias || ty.has_opaque_types() {
if ty.references_error() {
return;
}
} else {
bug!();
}
}
_ => bug!(),
}

let ty_predicates = tcx.predicates_of(item.owner_id);
Expand Down
28 changes: 25 additions & 3 deletions compiler/rustc_hir_analysis/src/variance/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use hir::def_id::{DefId, LocalDefId};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{GenericArgKind, GenericArgsRef};

use super::terms::VarianceTerm::*;
Expand Down Expand Up @@ -78,6 +78,12 @@ pub fn add_constraints_from_crate<'a, 'tcx>(
}
}
DefKind::Fn | DefKind::AssocFn => constraint_cx.build_constraints_for_item(def_id),
DefKind::TyAlias
if tcx.features().lazy_type_alias
|| tcx.type_of(def_id).instantiate_identity().has_opaque_types() =>
{
constraint_cx.build_constraints_for_item(def_id)
}
_ => {}
}
}
Expand All @@ -101,7 +107,18 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {

let inferred_start = self.terms_cx.inferred_starts[&def_id];
let current_item = &CurrentItem { inferred_start };
match tcx.type_of(def_id).instantiate_identity().kind() {
let ty = tcx.type_of(def_id).instantiate_identity();

// The type as returned by `type_of` is the underlying type and generally not a weak projection.
// Therefore we need to check the `DefKind` first.
if let DefKind::TyAlias = tcx.def_kind(def_id)
&& (tcx.features().lazy_type_alias || ty.has_opaque_types())
{
self.add_constraints_from_ty(current_item, ty, self.covariant);
return;
}

match ty.kind() {
ty::Adt(def, _) => {
// Not entirely obvious: constraints on structs/enums do not
// affect the variance of their type parameters. See discussion
Expand All @@ -127,6 +144,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
}

ty::Error(_) => {}

_ => {
span_bug!(
tcx.def_span(def_id),
Expand Down Expand Up @@ -252,10 +270,14 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
self.add_constraints_from_args(current, def.did(), args, variance);
}

ty::Alias(_, ref data) => {
ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, ref data) => {
self.add_constraints_from_invariant_args(current, data.args, variance);
}

ty::Alias(ty::Weak, ref data) => {
self.add_constraints_from_args(current, data.def_id, data.args, variance);
}

ty::Dynamic(data, r, _) => {
// The type `dyn Trait<T> +'a` is covariant w/r/t `'a`:
self.add_constraints_from_region(current, r, variance);
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_hir_analysis/src/variance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::query::Providers;
use rustc_middle::ty::{self, CrateVariancesMap, GenericArgsRef, Ty, TyCtxt};
use rustc_middle::ty::{TypeSuperVisitable, TypeVisitable};
use rustc_middle::ty::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt};
use std::ops::ControlFlow;

/// Defines the `TermsContext` basically houses an arena where we can
Expand Down Expand Up @@ -56,6 +56,14 @@ fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] {
let crate_map = tcx.crate_variances(());
return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]);
}
DefKind::TyAlias
if tcx.features().lazy_type_alias
|| tcx.type_of(item_def_id).instantiate_identity().has_opaque_types() =>
{
// These are inferred.
let crate_map = tcx.crate_variances(());
return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]);
}
DefKind::OpaqueTy => {
return variance_of_opaque(tcx, item_def_id);
}
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_hir_analysis/src/variance/terms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use rustc_arena::DroplessArena;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap};
use rustc_middle::ty::{self, TyCtxt};
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
use std::fmt;

use self::VarianceTerm::*;
Expand Down Expand Up @@ -97,6 +97,12 @@ pub fn determine_parameters_to_be_inferred<'a, 'tcx>(
}
}
DefKind::Fn | DefKind::AssocFn => terms_cx.add_inferreds_for_item(def_id),
DefKind::TyAlias
if tcx.features().lazy_type_alias
|| tcx.type_of(def_id).instantiate_identity().has_opaque_types() =>
{
terms_cx.add_inferreds_for_item(def_id)
}
_ => {}
}
}
Expand Down
Loading
Loading