Skip to content
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ jobs:
- name: Checkout the standard-library backtrace submodule
run: git submodule update --init --depth 1 library/backtrace

- name: Check compiler crates
- name: Check compiler crates and Clippy
run: >-
python3 x.py --ci false --set build.submodules=false --set rust.debug-assertions=true
--set rust.lto=off --warnings deny check compiler
--set rust.lto=off --warnings deny check compiler src/tools/clippy

- name: Install Ninja for Linux distribution tests
if: runner.os == 'Linux'
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/delegation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

// FIXME(fn_delegation): add default `..` for all other fields.
let initializer = hir::ExprKind::Struct(
self.arena.alloc(path),
self.arena.alloc(hir::StructQPath::new(path)),
self.arena.alloc_slice(&[hir::ExprField {
hir_id: self.next_id(),
is_shorthand: false,
Expand Down
28 changes: 20 additions & 8 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
StructRest::NoneWithError(guar) => hir::StructTailExpr::NoneWithError(guar),
};
hir::ExprKind::Struct(
self.arena.alloc(self.lower_qpath(
self.arena.alloc(hir::StructQPath::new(self.lower_qpath(
e.id,
&se.qself,
&se.path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
None,
)),
))),
self.arena.alloc_from_iter(se.fields.iter().map(|x| self.lower_expr_field(x))),
rest,
)
Expand Down Expand Up @@ -1418,7 +1418,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
StructRest::Rest(span) => Some(self.lower_span(*span)),
StructRest::None | StructRest::NoneWithError(_) => None,
};
let struct_pat = hir::PatKind::Struct(qpath, field_pats, fields_omitted);
let struct_pat = hir::PatKind::Struct(
self.arena.alloc(hir::StructQPath::new(qpath)),
field_pats,
fields_omitted,
);
return self.pat_without_dbm(lhs.span, struct_pat);
}
// Tuples.
Expand Down Expand Up @@ -1570,7 +1574,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
);

hir::ExprKind::Struct(
self.arena.alloc(self.make_lang_item_qpath(lang_item, span, None)),
self.arena
.alloc(hir::StructQPath::new(self.make_lang_item_qpath(lang_item, span, None))),
fields,
hir::StructTailExpr::None,
)
Expand Down Expand Up @@ -2153,16 +2158,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
pub(super) fn expr_struct(
&mut self,
span: Span,
path: &'hir hir::QPath<'hir>,
path: hir::QPath<'hir>,
fields: &'hir [hir::ExprField<'hir>],
) -> hir::Expr<'hir> {
self.expr(span, hir::ExprKind::Struct(path, fields, rustc_hir::StructTailExpr::None))
self.expr(
span,
hir::ExprKind::Struct(
self.arena.alloc(hir::StructQPath::new(path)),
fields,
rustc_hir::StructTailExpr::None,
),
)
}

pub(super) fn expr_enum_variant(
&mut self,
span: Span,
path: &'hir hir::QPath<'hir>,
path: hir::QPath<'hir>,
fields: &'hir [hir::Expr<'hir>],
) -> hir::Expr<'hir> {
let fields = self.arena.alloc_from_iter(fields.into_iter().enumerate().map(|(i, f)| {
Expand All @@ -2183,7 +2195,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
lang_item: LangItem,
fields: &'hir [hir::Expr<'hir>],
) -> hir::Expr<'hir> {
let path = self.arena.alloc(self.make_lang_item_qpath(lang_item, span, None));
let path = self.make_lang_item_qpath(lang_item, span, None);
self.expr_enum_variant(span, path, fields)
}

Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3221,7 +3221,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
fields: &'hir [hir::PatField<'hir>],
) -> &'hir hir::Pat<'hir> {
let path = self.make_lang_item_qpath(lang_item, self.lower_span(span), None);
self.pat(span, hir::PatKind::Struct(path, fields, None))
self.pat(
span,
hir::PatKind::Struct(self.arena.alloc(hir::StructQPath::new(path)), fields, None),
)
}

fn pat_ident(&mut self, span: Span, ident: Ident) -> (&'hir hir::Pat<'hir>, HirId) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}));
break hir::PatKind::Struct(
qpath,
self.arena.alloc(hir::StructQPath::new(qpath)),
fs,
match etc {
ast::PatFieldsRest::Rest(sp) => Some(self.lower_span(*sp)),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ impl<'diag, 'tcx> MirBorrowckCtxt<'_, 'diag, 'tcx> {
else {
return;
};
let hir::QPath::Resolved(_, path) = struct_qpath else { return };
let hir::QPath::Resolved(_, path) = struct_qpath.qpath else { return };
let hir::def::Res::Def(_, def_id) = path.res else { return };
let Some(expr_ty) = typeck_results.node_type_opt(expr.hir_id) else { return };
let ty::Adt(def, args) = expr_ty.kind() else { return };
Expand Down Expand Up @@ -2526,7 +2526,7 @@ impl<'diag, 'tcx> MirBorrowckCtxt<'_, 'diag, 'tcx> {
&& let hir::ExprKind::Path(qpath) = path.kind
&& self.tcx.qpath_is_lang_item(qpath, LangItem::IteratorNext)
&& let hir::PatKind::Struct(qpath, [field, ..], _) = bind.pat.kind
&& self.tcx.qpath_is_lang_item(qpath, LangItem::OptionSome)
&& self.tcx.qpath_is_lang_item(qpath.qpath, LangItem::OptionSome)
&& call.span.contains(self.issue_span)
{
// Find `<pat>` and the span for the whole `for` loop.
Expand Down
58 changes: 51 additions & 7 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ pub enum PatKind<'hir> {

/// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`).
/// The `Option` contains the span of a possible `..`.
Struct(QPath<'hir>, &'hir [PatField<'hir>], Option<Span>),
Struct(&'hir StructQPath<'hir>, &'hir [PatField<'hir>], Option<Span>),

/// A tuple struct/variant pattern `Variant(x, y, .., z)`.
/// If the `..` pattern fragment is present, then `DotDotPos` denotes its position.
Expand Down Expand Up @@ -2587,12 +2587,18 @@ impl Expr<'_> {
) => path1.res == path2.res,
(
ExprKind::Struct(
&QPath::Resolved(None, &Path { res: Res::Def(_, path1_def_id), .. }),
&crate::StructQPath {
qpath: QPath::Resolved(None, &Path { res: Res::Def(_, path1_def_id), .. }),
..
},
args1,
StructTailExpr::None,
),
ExprKind::Struct(
&QPath::Resolved(None, &Path { res: Res::Def(_, path2_def_id), .. }),
&crate::StructQPath {
qpath: QPath::Resolved(None, &Path { res: Res::Def(_, path2_def_id), .. }),
..
},
args2,
StructTailExpr::None,
),
Expand All @@ -2619,7 +2625,11 @@ impl Expr<'_> {
/// Checks if the specified expression is a built-in range literal.
/// (See: `LoweringContext::lower_expr()`).
pub fn is_range_literal(expr: &Expr<'_>) -> bool {
if let ExprKind::Struct(QPath::Resolved(None, path), _, StructTailExpr::None) = expr.kind
if let ExprKind::Struct(
crate::StructQPath { qpath: QPath::Resolved(None, path), .. },
_,
StructTailExpr::None,
) = expr.kind
&& let [.., segment] = path.segments
&& let sym::RangeFrom
| sym::RangeFull
Expand Down Expand Up @@ -2781,7 +2791,7 @@ pub enum ExprKind<'hir> {
///
/// E.g., `Foo {x: 1, y: 2}`, or `Foo {x: 1, .. base}`,
/// where `base` is the `Option<Expr>`.
Struct(&'hir QPath<'hir>, &'hir [ExprField<'hir>], StructTailExpr<'hir>),
Struct(&'hir StructQPath<'hir>, &'hir [ExprField<'hir>], StructTailExpr<'hir>),

/// An array literal constructed from one repeated element.
///
Expand Down Expand Up @@ -2862,6 +2872,39 @@ impl<'hir> QPath<'hir> {
}
}

/// The path of a struct literal or a struct pattern.
#[derive(Debug, Clone, Copy)]
pub struct StructQPath<'hir> {
pub qpath: QPath<'hir>,
pub(crate) joined_span: Span,
}

impl<'hir> StructQPath<'hir> {
pub fn new(qpath: QPath<'hir>) -> Self {
// Joining different syntax contexts can consult the metavariable span map.
// Keep that lookup at its original demand, including after the map freezes.
let joined_span = match qpath {
QPath::TypeRelative(qself, segment) if qself.span.eq_ctxt(segment.ident.span) => {
qself.span.to(segment.ident.span)
}
_ => DUMMY_SP,
};
Self { qpath, joined_span }
}

pub fn span(&self) -> Span {
if self.joined_span == DUMMY_SP { self.qpath.span() } else { self.joined_span }
}
}

impl<'hir> std::ops::Deref for StructQPath<'hir> {
type Target = QPath<'hir>;

fn deref(&self) -> &QPath<'hir> {
&self.qpath
}
}

/// Hints at the original code for a let statement.
#[derive(Copy, Clone, Debug, StableHash)]
pub enum LocalSource {
Expand Down Expand Up @@ -5388,14 +5431,15 @@ mod size_asserts {
static_assert_size!(ItemKind<'_>, 64);
static_assert_size!(LetStmt<'_>, 64);
static_assert_size!(Param<'_>, 32);
static_assert_size!(Pat<'_>, 80);
static_assert_size!(PatKind<'_>, 56);
static_assert_size!(Pat<'_>, 72);
static_assert_size!(PatKind<'_>, 48);
static_assert_size!(Path<'_>, 40);
static_assert_size!(PathSegment<'_>, 48);
static_assert_size!(QPath<'_>, 24);
static_assert_size!(Res, 12);
static_assert_size!(Stmt<'_>, 32);
static_assert_size!(StmtKind<'_>, 16);
static_assert_size!(StructQPath<'_>, 32);
static_assert_size!(TraitImplHeader<'_>, 48);
static_assert_size!(TraitItem<'_>, 88);
static_assert_size!(TraitItemKind<'_>, 48);
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_hir/src/pat_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ impl hir::Pat<'_> {
..
})
| PatKind::TupleStruct(hir::QPath::Resolved(_, path), ..)
| PatKind::Struct(hir::QPath::Resolved(_, path), ..) => {
| PatKind::Struct(
crate::StructQPath { qpath: hir::QPath::Resolved(_, path), .. },
..,
) => {
if let Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Variant, ..), id) =
path.res
{
Expand Down
13 changes: 12 additions & 1 deletion compiler/rustc_hir/src/stable_hash_impls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_data_structures::stable_hash::{StableHash, StableHashCtxt, StableHasher};

use crate::hir::{AttributeMap, OwnerInfo, OwnerNodes};
use crate::hir::{AttributeMap, OwnerInfo, OwnerNodes, QPath, StructQPath};

// The following implementations of StableHash for `ItemId`, `TraitItemId`, and
// `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
Expand Down Expand Up @@ -39,3 +39,14 @@ impl<'tcx> StableHash for OwnerInfo<'tcx> {
opt_hash.unwrap().stable_hash(hcx, hasher);
}
}

impl StableHash for StructQPath<'_> {
fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
self.qpath.stable_hash(hcx, hasher);
if let QPath::TypeRelative(..) = self.qpath {
// Two independently moving parents can change the joined geometry
// even when both source-relative operand fingerprints stay equal.
self.joined_span.stable_hash(hcx, hasher);
}
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&& let ExprKind::Block(block, _) = then_expr.kind
// Refutability checks occur on the MIR, so we approximate it here by checking
// if we have an enum with a single variant or a struct in the pattern.
&& let PatKind::TupleStruct(qpath, ..) | PatKind::Struct(qpath, ..) = pat.kind
&& let PatKind::TupleStruct(qpath, ..) | PatKind::Struct(&hir::StructQPath { qpath, .. }, ..) = pat.kind
&& let hir::QPath::Resolved(_, path) = qpath
{
match path.res {
Expand Down
16 changes: 12 additions & 4 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,10 +1202,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) {
match expr.peel_drop_temps().kind {
hir::ExprKind::Struct(
hir::QPath::Resolved(
None,
hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, span, .. },
),
rustc_hir::StructQPath {
qpath:
hir::QPath::Resolved(
None,
hir::Path {
res: hir::def::Res::SelfTyAlias { alias_to, .. },
span,
..
},
),
..
},
..,
)
| hir::ExprKind::Call(
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self,
expr: &hir::Expr<'tcx>,
expected: Expectation<'tcx>,
qpath: &'tcx QPath<'tcx>,
qpath: &'tcx hir::StructQPath<'tcx>,
fields: &'tcx [hir::ExprField<'tcx>],
base_expr: &'tcx hir::StructTailExpr<'tcx>,
) -> Ty<'tcx> {
Expand Down Expand Up @@ -2440,7 +2440,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
mut err: Diag<'_>,
) {
if is_range_literal(last_expr_field.expr)
&& let ExprKind::Struct(&qpath, [range_start, range_end], _) = last_expr_field.expr.kind
&& let ExprKind::Struct(&hir::StructQPath { qpath, .. }, [range_start, range_end], _) =
last_expr_field.expr.kind
&& self.tcx.qpath_is_lang_item(qpath, LangItem::Range)
&& let variant_field =
variant.fields.iter().find(|field| field.ident(self.tcx) == last_expr_field.ident)
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

pub(crate) fn check_struct_path(
&self,
qpath: &QPath<'tcx>,
qpath: &hir::StructQPath<'tcx>,
hir_id: HirId,
) -> Result<(&'tcx ty::VariantDef, Ty<'tcx>), ErrorGuaranteed> {
let path_span = qpath.span();
Expand Down Expand Up @@ -2384,8 +2384,8 @@ impl<'a, 'tcx> FnCallDiagCtxt<'a, 'tcx> {
if let ty::Adt(adt, _) = ty.kind()
&& self.tcx().is_lang_item(adt.did(), LangItem::RangeFull)
&& is_range_literal(expr)
&& let hir::ExprKind::Struct(&path, [], _) = expr.kind
&& self.tcx().qpath_is_lang_item(path, LangItem::RangeFull)
&& let hir::ExprKind::Struct(path, [], _) = expr.kind
&& self.tcx().qpath_is_lang_item(path.qpath, LangItem::RangeFull)
{
// We have `Foo(a, .., c)`, where the user might be trying to use the "rest" syntax
// from default field values, which is not supported on tuples.
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return false;
}
match expr.kind {
ExprKind::Struct(&qpath, [start, end], _)
ExprKind::Struct(&hir::StructQPath { qpath, .. }, [start, end], _)
if is_range_literal(expr)
&& self.tcx.qpath_is_lang_item(qpath, LangItem::Range) =>
{
Expand All @@ -1844,7 +1844,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
true
}
ExprKind::Struct(&qpath, [arg], _)
ExprKind::Struct(&hir::StructQPath { qpath, .. }, [arg], _)
if is_range_literal(expr)
&& let Some(qpath @ (LangItem::RangeFrom | LangItem::RangeTo)) =
self.tcx.qpath_lang_item(qpath) =>
Expand Down Expand Up @@ -3794,7 +3794,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if !hir::is_range_literal(expr) {
return;
}
let hir::ExprKind::Struct(&qpath, [start, end], _) = expr.kind else {
let hir::ExprKind::Struct(&hir::StructQPath { qpath, .. }, [start, end], _) = expr.kind
else {
return;
};
if !self.tcx.qpath_is_lang_item(qpath, LangItem::Range) {
Expand Down
Loading
Loading