Skip to content

Commit ab8058a

Browse files
committed
Auto merge of #160974 - JonathanBrouwer:rollup-9P4088l, r=JonathanBrouwer
Rollup of 5 pull requests Successful merges: - #160900 (Ensure that restriction paths are ancestors) - #155846 (tests/ui/tuple: add annotations for reference rules) - #160890 (Disable some tests for ohos target) - #160952 (Fix improper stability of `Write for Cursor<W: WriteThroughCursor>`) - #160970 (Fix handling of relative paths starting with a dot in bootstrap)
2 parents 41fb9d4 + f211f6a commit ab8058a

36 files changed

Lines changed: 302 additions & 200 deletions

compiler/rustc_ast_lowering/src/diagnostics.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_errors::codes::*;
2-
use rustc_errors::{DiagArgFromDisplay, DiagSymbolList};
2+
use rustc_errors::{DiagArgFromDisplay, DiagArgValue, DiagSymbolList, IntoDiagArg};
33
use rustc_macros::{Diagnostic, Subdiagnostic};
44
use rustc_span::{Ident, Span, Symbol};
55

@@ -579,3 +579,33 @@ pub(crate) struct DelegationAttemptedBlockWithDefsRelowering {
579579
#[primary_span]
580580
pub span: Span,
581581
}
582+
583+
/// Whether resolving `impl` or `mut` restriction paths
584+
#[derive(Debug, Clone, Copy)]
585+
pub(crate) enum ResolvingRestrictionKind {
586+
Impl,
587+
Mut,
588+
}
589+
590+
impl IntoDiagArg for ResolvingRestrictionKind {
591+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
592+
use std::borrow::Cow;
593+
match self {
594+
ResolvingRestrictionKind::Impl => DiagArgValue::Str(Cow::Borrowed("impl")),
595+
ResolvingRestrictionKind::Mut => DiagArgValue::Str(Cow::Borrowed("mut")),
596+
}
597+
}
598+
}
599+
600+
#[derive(Diagnostic)]
601+
#[diag(
602+
"{$kind ->
603+
[impl] trait implementation
604+
*[mut] field mutation
605+
} can only be restricted to ancestor modules"
606+
)]
607+
pub(crate) struct RestrictionAncestorOnly {
608+
#[primary_span]
609+
pub(crate) span: Span,
610+
pub(crate) kind: ResolvingRestrictionKind,
611+
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use super::{
2626
FnDeclKind, GenericArgsMode, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
2727
RelaxedBoundForbiddenReason, RelaxedBoundPolicy,
2828
};
29-
use crate::diagnostics::ConstComptimeFn;
29+
use crate::diagnostics::{ConstComptimeFn, ResolvingRestrictionKind, RestrictionAncestorOnly};
3030

3131
pub(super) struct ItemLowerer<'a, 'hir> {
3232
pub(super) tcx: TyCtxt<'hir>,
@@ -498,7 +498,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
498498
items,
499499
}) => {
500500
let constness = self.lower_constness(attrs, *constness);
501-
let impl_restriction = self.lower_impl_restriction(impl_restriction);
501+
let impl_restriction = self.lower_impl_restriction(impl_restriction, hir_id);
502502
let ident = self.lower_ident(*ident);
503503
let (generics, (safety, items, bounds)) = self.lower_generics(
504504
generics,
@@ -895,7 +895,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
895895
None => Ident::new(sym::integer(index), self.lower_span(f.span)),
896896
},
897897
vis_span: self.lower_span(f.vis.span),
898-
mut_restriction: self.lower_mut_restriction(f.mut_restriction()),
898+
mut_restriction: self.lower_mut_restriction(f.mut_restriction(), hir_id),
899899
default: f
900900
.default_value()
901901
.map(|v| self.lower_anon_const_to_anon_const(v, v.value.span)),
@@ -1797,26 +1797,46 @@ impl<'hir> LoweringContext<'_, 'hir> {
17971797
}
17981798
}
17991799

1800-
fn lower_restriction_kind(&mut self, kind: &RestrictionKind) -> hir::RestrictionKind<'hir> {
1801-
match kind {
1800+
fn lower_restriction_kind(
1801+
&mut self,
1802+
restriction_kind: &RestrictionKind,
1803+
hir_id: HirId,
1804+
resolving_kind: ResolvingRestrictionKind,
1805+
) -> hir::RestrictionKind<'hir> {
1806+
match restriction_kind {
18021807
RestrictionKind::Unrestricted => hir::RestrictionKind::Unrestricted,
18031808
RestrictionKind::Restricted { path, id, shorthand: _ } => {
18041809
let res = self.get_partial_res(*id);
1810+
let parent_module = self.tcx.parent_module(hir_id);
18051811
if let Some(did) = res.and_then(|res| res.expect_full_res().opt_def_id()) {
1806-
hir::RestrictionKind::Restricted(self.arena.alloc(hir::Path {
1807-
res: did,
1808-
segments: self.arena.alloc_from_iter(path.segments.iter().map(|segment| {
1809-
self.lower_path_segment(
1810-
path.span,
1811-
segment,
1812-
ParamMode::Explicit,
1813-
GenericArgsMode::Err,
1814-
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1815-
None,
1816-
)
1817-
})),
1818-
span: self.lower_span(path.span),
1819-
}))
1812+
if !self.tcx.is_descendant_of(parent_module, did) {
1813+
// If the restriction path is not an ancestor of the item,
1814+
// emit an error and recover by lowering the restriction to `Unrestricted`.
1815+
self.dcx()
1816+
.create_err(RestrictionAncestorOnly {
1817+
span: path.span,
1818+
kind: resolving_kind,
1819+
})
1820+
.emit();
1821+
hir::RestrictionKind::Unrestricted
1822+
} else {
1823+
hir::RestrictionKind::Restricted(self.arena.alloc(hir::Path {
1824+
res: did,
1825+
segments: self.arena.alloc_from_iter(path.segments.iter().map(
1826+
|segment| {
1827+
self.lower_path_segment(
1828+
path.span,
1829+
segment,
1830+
ParamMode::Explicit,
1831+
GenericArgsMode::Err,
1832+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1833+
None,
1834+
)
1835+
},
1836+
)),
1837+
span: self.lower_span(path.span),
1838+
}))
1839+
}
18201840
} else {
18211841
self.dcx().span_delayed_bug(path.span, "should have errored in resolve");
18221842
hir::RestrictionKind::Unrestricted
@@ -1828,16 +1848,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
18281848
pub(super) fn lower_impl_restriction(
18291849
&mut self,
18301850
r: &ImplRestriction,
1851+
hir_id: HirId,
18311852
) -> &'hir hir::ImplRestriction<'hir> {
1832-
let kind = self.lower_restriction_kind(&r.kind);
1853+
let kind = self.lower_restriction_kind(&r.kind, hir_id, ResolvingRestrictionKind::Impl);
18331854
self.arena.alloc(hir::ImplRestriction { kind, span: self.lower_span(r.span) })
18341855
}
18351856

18361857
pub(super) fn lower_mut_restriction(
18371858
&mut self,
18381859
r: &MutRestriction,
1860+
hir_id: HirId,
18391861
) -> &'hir hir::MutRestriction<'hir> {
1840-
let kind = self.lower_restriction_kind(&r.kind);
1862+
let kind = self.lower_restriction_kind(&r.kind, hir_id, ResolvingRestrictionKind::Mut);
18411863
self.arena.alloc(hir::MutRestriction { kind, span: self.lower_span(r.span) })
18421864
}
18431865

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4398,6 +4398,8 @@ pub enum RestrictionKind<'hir> {
43984398
/// The restriction does not affect the item.
43994399
Unrestricted,
44004400
/// The restriction only applies outside of this path.
4401+
/// The path is guaranteed to resolve to an ancestor module
4402+
/// of the restricted item.
44014403
Restricted(&'hir Path<'hir, DefId>),
44024404
}
44034405

compiler/rustc_resolve/src/diagnostics/mod.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_macros::{Diagnostic, Subdiagnostic};
88
use rustc_span::{Ident, Span, Spanned, Symbol};
99

1010
use crate::Res;
11-
use crate::late::{PatternSource, ResolvingRestrictionKind};
11+
use crate::late::PatternSource;
1212

1313
pub(crate) mod impls;
1414

@@ -547,19 +547,6 @@ pub(crate) struct ExpectedModuleFound {
547547
#[diag("cannot determine resolution for the visibility", code = E0578)]
548548
pub(crate) struct Indeterminate(#[primary_span] pub(crate) Span);
549549

550-
#[derive(Diagnostic)]
551-
#[diag(
552-
"{$kind ->
553-
[impl] trait implementation
554-
*[mut] field mutation
555-
} can only be restricted to ancestor modules"
556-
)]
557-
pub(crate) struct RestrictionAncestorOnly {
558-
#[primary_span]
559-
pub(crate) span: Span,
560-
pub(crate) kind: ResolvingRestrictionKind,
561-
}
562-
563550
#[derive(Diagnostic)]
564551
#[diag("cannot use a tool module through an import")]
565552
pub(crate) struct ToolModuleImported {

compiler/rustc_resolve/src/late.rs

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -426,23 +426,6 @@ pub(crate) enum AliasPossibility {
426426
Maybe,
427427
}
428428

429-
/// Whether resolving `impl` or `mut` restriction paths
430-
#[derive(Debug, Clone, Copy)]
431-
pub(crate) enum ResolvingRestrictionKind {
432-
Impl,
433-
Mut,
434-
}
435-
436-
impl IntoDiagArg for ResolvingRestrictionKind {
437-
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
438-
use std::borrow::Cow;
439-
match self {
440-
ResolvingRestrictionKind::Impl => DiagArgValue::Str(Cow::Borrowed("impl")),
441-
ResolvingRestrictionKind::Mut => DiagArgValue::Str(Cow::Borrowed("mut")),
442-
}
443-
}
444-
}
445-
446429
#[derive(Copy, Clone, Debug)]
447430
pub(crate) enum PathSource<'a, 'ast, 'ra> {
448431
/// Type paths `Path`.
@@ -1502,7 +1485,7 @@ impl<'ast, 'ra, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'ra, 'tc
15021485
let FieldDef { attrs, id: _, span: _, vis, ident, ty, is_placeholder: _, extras: _ } = f;
15031486
walk_list!(self, visit_attribute, attrs);
15041487
try_visit!(self.visit_vis(vis));
1505-
self.resolve_restriction_path(&f.mut_restriction().kind, ResolvingRestrictionKind::Mut);
1488+
self.resolve_restriction_path(&f.mut_restriction().kind);
15061489
visit_opt!(self, visit_ident, ident);
15071490
try_visit!(self.visit_ty(ty));
15081491
if let Some(v) = f.default_value() {
@@ -2875,10 +2858,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28752858

28762859
ItemKind::Trait(Trait { generics, bounds, items, impl_restriction, .. }) => {
28772860
// resolve paths for `impl` restrictions
2878-
self.resolve_restriction_path(
2879-
&impl_restriction.kind,
2880-
ResolvingRestrictionKind::Impl,
2881-
);
2861+
self.resolve_restriction_path(&impl_restriction.kind);
28822862

28832863
// Create a new rib for the trait-wide type parameters.
28842864
self.with_generic_param_rib(
@@ -4494,31 +4474,11 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
44944474
}
44954475
}
44964476

4497-
fn resolve_restriction_path(
4498-
&mut self,
4499-
restriction: &'ast ast::RestrictionKind,
4500-
kind: ResolvingRestrictionKind,
4501-
) {
4477+
fn resolve_restriction_path(&mut self, restriction: &'ast ast::RestrictionKind) {
45024478
match &restriction {
45034479
ast::RestrictionKind::Unrestricted => (),
45044480
ast::RestrictionKind::Restricted { path, id, shorthand: _ } => {
45054481
self.smart_resolve_path(*id, &None, path, PathSource::Module);
4506-
if let Some(res) = self.r.partial_res_map[&id].full_res()
4507-
&& let Some(def_id) = res.opt_def_id()
4508-
{
4509-
if !self.r.is_accessible_from(
4510-
Visibility::Restricted(def_id),
4511-
self.parent_scope.module,
4512-
) {
4513-
self.r
4514-
.dcx()
4515-
.create_err(crate::diagnostics::RestrictionAncestorOnly {
4516-
span: path.span,
4517-
kind,
4518-
})
4519-
.emit();
4520-
}
4521-
}
45224482
}
45234483
}
45244484
}

library/core/src/io/cursor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ pub trait WriteThroughCursor: Sized {
486486
}
487487

488488
#[doc(hidden)]
489+
#[stable(feature = "rust1", since = "1.0.0")]
489490
impl<W: WriteThroughCursor> Write for Cursor<W> {
490491
#[inline]
491492
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {

src/bootstrap/src/core/builder/cli_paths.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,30 @@ pub(crate) fn match_paths_to_steps_and_run(
8383
// repository root, to match the paths registered by command-line steps.
8484
//
8585
// E.g. `/home/ferris/rust/tests/ui/asm/cfg.rs` => `tests/ui/asm/cfg.rs`
86+
//
87+
// It is also possible that someone passed a relative path starting with . or ..
88+
// In that case, we have to remove that path prefix.
8689
let mut paths = paths
8790
.iter()
8891
.map(|path| {
92+
// Here we "launder" the path through builder.src, to normalize relative path prefixes
93+
// so ./tests/foo becomes just tests/foo
94+
let path = if path.is_relative() {
95+
builder
96+
.src
97+
.join(path)
98+
.strip_prefix(&builder.src)
99+
.expect("Cannot strip src path prefix")
100+
.to_path_buf()
101+
} else {
102+
path.to_path_buf()
103+
};
104+
89105
if path.is_absolute()
90106
&& path.exists()
91107
&& let Ok(relative) = path.strip_prefix(&builder.src)
92108
{
93-
relative
109+
relative.to_path_buf()
94110
} else {
95111
path
96112
}
@@ -101,7 +117,9 @@ pub(crate) fn match_paths_to_steps_and_run(
101117
// If any absolute paths couldn't be made relative, stop now and report them.
102118
let bad_abs_paths = paths.iter().filter(|path| path.is_absolute()).collect::<Vec<_>>();
103119
if !bad_abs_paths.is_empty() {
104-
eprintln!("ERROR: failed to resolve absolute paths: {bad_abs_paths:#?}");
120+
eprintln!(
121+
"ERROR: the following paths do not exist on disk or point outside the source directory: {bad_abs_paths:#?}"
122+
);
105123
crate::exit!(1);
106124
}
107125

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: src/bootstrap/src/core/builder/cli_paths/tests.rs
3+
expression: test ./tests/ui
4+
---
5+
[Test] test::Ui
6+
targets: [aarch64-unknown-linux-gnu]
7+
- Suite(tests/ui)

src/bootstrap/src/core/builder/cli_paths/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ declare_tests!(
199199
(x_test_tests, "test tests"),
200200
(x_test_tests_skip_coverage, "test tests --skip=coverage"),
201201
(x_test_tests_ui, "test tests/ui"),
202+
(x_test_tests_ui_dot_prefix, "test ./tests/ui"),
202203
(x_test_tidy, "test tidy"),
203204
(x_test_tidyselftest, "test tidyselftest"),
204205
(x_test_ui, "test ui"),

src/tools/compiletest/src/directives/directive_names.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub(crate) const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
104104
"ignore-nto",
105105
"ignore-nvptx64",
106106
"ignore-nvptx64-nvidia-cuda",
107+
"ignore-ohos",
107108
"ignore-openbsd",
108109
"ignore-parallel-frontend",
109110
"ignore-pauthtest",

0 commit comments

Comments
 (0)