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 10 pull requests #126037

Merged
merged 35 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ad7c2b0
Updated error code explanation
OliverKillane May 5, 2024
f3dcf65
fix whitespace
OliverKillane May 5, 2024
a5d0988
Update compiler/rustc_error_codes/src/error_codes/E0582.md
OliverKillane May 14, 2024
012288b
tidy fix from suggestion
OliverKillane May 15, 2024
d6e4fe5
A custom error message for lending iterators
pacak May 22, 2024
b70fb41
And more general error
pacak May 24, 2024
9f8b1ca
Add intra-doc-links to rustc_middle crate-level docs.
aDotInTheVoid May 24, 2024
e485b19
Don't drop Upcast candidate in intercrate mode
compiler-errors May 30, 2024
5e802f0
rustc_codegen_ssa: fix get_rpath_relative_to_output panic when lib on…
name1e5s Jun 1, 2024
92f85f1
wipe bootstrap build before switching to bumped rustc
onur-ozkan Jun 3, 2024
e609c9b
Add unit tests for `Span::trim_start`
Zalathar Jun 3, 2024
df96cba
Add `Span::trim_end`
Zalathar Jun 3, 2024
9c931c0
coverage: Return a nested vector from initial span extraction
Zalathar Jun 3, 2024
464dee2
coverage: Build up initial spans by appending to a vector
Zalathar Jun 3, 2024
6d1557f
coverage: Use hole spans to carve up coverage spans into separate buc…
Zalathar Jun 3, 2024
c57a1d1
coverage: Remove hole-carving code from the main span refiner
Zalathar Jun 2, 2024
fd648a3
std::unix::fs::get_path: using fcntl codepath for netbsd instead.
devnexen Jun 3, 2024
dc020ae
Use a `LocalDefId` for `HirTyLowerer::item_def_id`, since we only eve…
oli-obk May 29, 2024
9d387d1
Simplify some code paths and remove an unused field
oli-obk May 31, 2024
abd308b
Remove an `Option` and instead eagerly create error lifetimes
oli-obk May 31, 2024
6a2e15a
Only collect infer vars to error about in case infer vars are actuall…
oli-obk May 31, 2024
7870674
Remove `allows_infer` now that every use of it is delegated to `HirTy…
oli-obk May 31, 2024
4146b82
Remove some unnecessary explicit lifetimes
oli-obk May 31, 2024
c8a331a
Unify optional param info with object lifetime default boolean into a…
oli-obk Jun 4, 2024
a8e091d
bivariant alias: set `has_unconstrained_ty_var`
lcnr Jun 5, 2024
28deff4
Rollup merge of #124746 - OliverKillane:E0582-explain-assoc-types-imp…
matthiaskrgr Jun 5, 2024
49f9504
Rollup merge of #125407 - pacak:no-lending-iterators, r=pnkfelix
matthiaskrgr Jun 5, 2024
efc3fdc
Rollup merge of #125505 - aDotInTheVoid:middle-idl, r=pnkfelix
matthiaskrgr Jun 5, 2024
e174512
Rollup merge of #125792 - compiler-errors:dont-drop-upcast-cand, r=lcnr
matthiaskrgr Jun 5, 2024
94c19c6
Rollup merge of #125819 - oli-obk:localize, r=fmease
matthiaskrgr Jun 5, 2024
e2ea7d8
Rollup merge of #125861 - name1e5s:fix/rpath_null_panic, r=michaelwoe…
matthiaskrgr Jun 5, 2024
ebc66fd
Rollup merge of #125911 - onur-ozkan:wipe-broken-cache, r=albertlarsan68
matthiaskrgr Jun 5, 2024
79bb336
Rollup merge of #125921 - Zalathar:buckets, r=oli-obk
matthiaskrgr Jun 5, 2024
808ad60
Rollup merge of #125940 - devnexen:unix_fs_netbsd_get_path, r=cuviper
matthiaskrgr Jun 5, 2024
dd2e1a3
Rollup merge of #126022 - lcnr:generalize-alias-bivariant, r=compiler…
matthiaskrgr Jun 5, 2024
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
5 changes: 5 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/rpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ fn get_rpath_relative_to_output(config: &RPathConfig<'_>, lib: &Path) -> OsStrin
// Strip filenames
let lib = lib.parent().unwrap();
let output = config.out_filename.parent().unwrap();

// If output or lib is empty, just assume it locates in current path
let lib = if lib == Path::new("") { Path::new(".") } else { lib };
let output = if output == Path::new("") { Path::new(".") } else { output };

let lib = try_canonicalize(lib).unwrap();
let output = try_canonicalize(output).unwrap();
let relative = path_relative_from(&lib, &output)
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/rpath/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ fn test_rpath_relative() {
}
}

#[test]
fn test_rpath_relative_issue_119571() {
let config = &mut RPathConfig {
libs: &[],
out_filename: PathBuf::from("rustc"),
has_rpath: true,
is_like_osx: false,
linker_is_gnu: true,
};
// Should not panic when out_filename only contains filename.
// Issue 119571
let _ = get_rpath_relative_to_output(config, Path::new("lib/libstd.so"));
// Should not panic when lib only contains filename.
let _ = get_rpath_relative_to_output(config, Path::new("libstd.so"));
}

#[test]
fn test_xlinker() {
let args = rpaths_to_flags(vec!["a/normal/path".into(), "a,comma,path".into()]);
Expand Down
34 changes: 34 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0582.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,40 @@ fn bar<F, G>(t: F, u: G)
fn main() { }
```

This error also includes the use of associated types with lifetime parameters.
```compile_fail,E0582
trait Foo {
type Assoc<'a>;
}

struct Bar<X, F>
where
X: Foo,
F: for<'a> Fn(X::Assoc<'a>) -> &'a i32
{
x: X,
f: F
}
```
The latter scenario encounters this error because `Foo::Assoc<'a>` could be
implemented by a type that does not use the `'a` parameter, so there is no
guarentee that `X::Assoc<'a>` actually uses `'a`.

To fix this we can pass a dummy parameter:
```
# trait Foo {
# type Assoc<'a>;
# }
struct Bar<X, F>
where
X: Foo,
F: for<'a> Fn(X::Assoc<'a>, /* dummy */ &'a ()) -> &'a i32
{
x: X,
f: F
}
```

Note: The examples above used to be (erroneously) accepted by the
compiler, but this was since corrected. See [issue #33685] for more
details.
Expand Down
117 changes: 105 additions & 12 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ use rustc_ast::Recovered;
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_data_structures::unord::UnordMap;
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey};
use rustc_hir as hir;
use rustc_errors::{struct_span_code_err, Applicability, Diag, ErrorGuaranteed, StashKey, E0228};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::intravisit::{self, walk_generics, Visitor};
use rustc_hir::{self as hir};
use rustc_hir::{GenericParamKind, Node};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::ObligationCause;
Expand All @@ -44,7 +44,7 @@ use std::ops::Bound;

use crate::check::intrinsic::intrinsic_operation_unsafety;
use crate::errors;
use crate::hir_ty_lowering::HirTyLowerer;
use crate::hir_ty_lowering::{HirTyLowerer, RegionInferReason};
pub use type_of::test_opaque_hidden_types;

mod generics_of;
Expand Down Expand Up @@ -370,16 +370,26 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
self.tcx
}

fn item_def_id(&self) -> DefId {
self.item_def_id.to_def_id()
fn item_def_id(&self) -> LocalDefId {
self.item_def_id
}

fn allow_infer(&self) -> bool {
false
}

fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
None
fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx> {
if let RegionInferReason::BorrowedObjectLifetimeDefault = reason {
let e = struct_span_code_err!(
self.tcx().dcx(),
span,
E0228,
"the lifetime bound for this object type cannot be deduced \
from context; please supply an explicit bound"
)
.emit();
self.set_tainted_by_errors(e);
ty::Region::new_error(self.tcx(), e)
} else {
// This indicates an illegal lifetime in a non-assoc-trait position
ty::Region::new_error_with_message(self.tcx(), span, "unelided lifetime in signature")
}
}

fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
Expand Down Expand Up @@ -510,6 +520,89 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
fn set_tainted_by_errors(&self, err: ErrorGuaranteed) {
self.tainted_by_errors.set(Some(err));
}

fn lower_fn_sig(
&self,
decl: &hir::FnDecl<'tcx>,
generics: Option<&hir::Generics<'_>>,
hir_id: rustc_hir::HirId,
hir_ty: Option<&hir::Ty<'_>>,
) -> (Vec<Ty<'tcx>>, Ty<'tcx>) {
let tcx = self.tcx();
// We proactively collect all the inferred type params to emit a single error per fn def.
let mut visitor = HirPlaceholderCollector::default();
let mut infer_replacements = vec![];

if let Some(generics) = generics {
walk_generics(&mut visitor, generics);
}

let input_tys = decl
.inputs
.iter()
.enumerate()
.map(|(i, a)| {
if let hir::TyKind::Infer = a.kind {
if let Some(suggested_ty) =
self.lowerer().suggest_trait_fn_ty_for_impl_fn_infer(hir_id, Some(i))
{
infer_replacements.push((a.span, suggested_ty.to_string()));
return Ty::new_error_with_message(tcx, a.span, suggested_ty.to_string());
}
}

// Only visit the type looking for `_` if we didn't fix the type above
visitor.visit_ty(a);
self.lowerer().lower_arg_ty(a, None)
})
.collect();

let output_ty = match decl.output {
hir::FnRetTy::Return(output) => {
if let hir::TyKind::Infer = output.kind
&& let Some(suggested_ty) =
self.lowerer().suggest_trait_fn_ty_for_impl_fn_infer(hir_id, None)
{
infer_replacements.push((output.span, suggested_ty.to_string()));
Ty::new_error_with_message(tcx, output.span, suggested_ty.to_string())
} else {
visitor.visit_ty(output);
self.lower_ty(output)
}
}
hir::FnRetTy::DefaultReturn(..) => tcx.types.unit,
};

if !(visitor.0.is_empty() && infer_replacements.is_empty()) {
// We check for the presence of
// `ident_span` to not emit an error twice when we have `fn foo(_: fn() -> _)`.

let mut diag = crate::collect::placeholder_type_error_diag(
tcx,
generics,
visitor.0,
infer_replacements.iter().map(|(s, _)| *s).collect(),
true,
hir_ty,
"function",
);

if !infer_replacements.is_empty() {
diag.multipart_suggestion(
format!(
"try replacing `_` with the type{} in the corresponding trait method signature",
rustc_errors::pluralize!(infer_replacements.len()),
),
infer_replacements,
Applicability::MachineApplicable,
);
}

self.set_tainted_by_errors(diag.emit());
}

(input_tys, output_ty)
}
}

/// Synthesize a new lifetime name that doesn't clash with any of the lifetimes already present.
Expand Down
14 changes: 8 additions & 6 deletions compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::bounds::Bounds;
use crate::collect::ItemCtxt;
use crate::constrained_generic_params as cgp;
use crate::hir_ty_lowering::{HirTyLowerer, OnlySelfBounds, PredicateFilter};
use crate::hir_ty_lowering::{HirTyLowerer, OnlySelfBounds, PredicateFilter, RegionInferReason};
use hir::{HirId, Node};
use rustc_data_structures::fx::FxIndexSet;
use rustc_hir as hir;
Expand Down Expand Up @@ -117,7 +117,6 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
let mut is_trait = None;
let mut is_default_impl_trait = None;

// FIXME: Should ItemCtxt take a LocalDefId?
let icx = ItemCtxt::new(tcx, def_id);

const NO_GENERICS: &hir::Generics<'_> = hir::Generics::empty();
Expand Down Expand Up @@ -244,12 +243,15 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
}

hir::WherePredicate::RegionPredicate(region_pred) => {
let r1 = icx.lowerer().lower_lifetime(region_pred.lifetime, None);
let r1 = icx
.lowerer()
.lower_lifetime(region_pred.lifetime, RegionInferReason::RegionPredicate);
predicates.extend(region_pred.bounds.iter().map(|bound| {
let (r2, span) = match bound {
hir::GenericBound::Outlives(lt) => {
(icx.lowerer().lower_lifetime(lt, None), lt.ident.span)
}
hir::GenericBound::Outlives(lt) => (
icx.lowerer().lower_lifetime(lt, RegionInferReason::RegionPredicate),
lt.ident.span,
),
bound => {
span_bug!(
bound.span(),
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::bounds::Bounds;
use crate::errors;
use crate::hir_ty_lowering::{HirTyLowerer, OnlySelfBounds, PredicateFilter};

use super::RegionInferReason;

impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
/// Add a `Sized` bound to the `bounds` if appropriate.
///
Expand Down Expand Up @@ -166,7 +168,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
}
hir::GenericBound::Outlives(lifetime) => {
let region = self.lower_lifetime(lifetime, None);
let region = self.lower_lifetime(lifetime, RegionInferReason::OutlivesBound);
bounds.push_region_bound(
self.tcx(),
ty::Binder::bind_with_vars(
Expand Down
Loading
Loading