Skip to content

Commit 82c2c8d

Browse files
committed
Update (doc) comments
Several (doc) comments were super outdated or didn't provide enough context. Some doc comments shoved everything in a single paragraph without respecting the fact that the first paragraph should be a single sentence because rustdoc treats these as item descriptions / synopses on module pages.
1 parent 05d48b9 commit 82c2c8d

File tree

36 files changed

+332
-252
lines changed

36 files changed

+332
-252
lines changed

compiler/rustc_ast/src/ast.rs

-4
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,6 @@ impl TraitBoundModifiers {
303303
};
304304
}
305305

306-
/// The AST represents all type param bounds as types.
307-
/// `typeck::collect::compute_bounds` matches these against
308-
/// the "special" built-in traits (see `middle::lang_items`) and
309-
/// detects `Copy`, `Send` and `Sync`.
310306
#[derive(Clone, Encodable, Decodable, Debug)]
311307
pub enum GenericBound {
312308
Trait(PolyTraitRef, TraitBoundModifiers),

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ pub fn walk_use_tree<'a, V: Visitor<'a>>(
480480
try_visit!(visitor.visit_path(&use_tree.prefix, id));
481481
match use_tree.kind {
482482
UseTreeKind::Simple(rename) => {
483-
// The extra IDs are handled during HIR lowering.
483+
// The extra IDs are handled during AST lowering.
484484
visit_opt!(visitor, visit_ident, rename);
485485
}
486486
UseTreeKind::Glob => {}

compiler/rustc_ast_lowering/src/delegation.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
//! item id (`item_id`) in case of impl trait or path resolution id (`path_id`) otherwise.
3030
//!
3131
//! Since we do not have a proper way to obtain function type information by path resolution
32-
//! in AST, we mark each function parameter type as `InferDelegation` and inherit it in `AstConv`.
32+
//! in AST, we mark each function parameter type as `InferDelegation` and inherit it during
33+
//! HIR ty lowering.
3334
//!
3435
//! Similarly generics, predicates and header are set to the "default" values.
3536
//! In case of discrepancy with callee function the `NotSupportedDelegation` error will
36-
//! also be emitted in `AstConv`.
37+
//! also be emitted during HIR ty lowering.
3738
3839
use crate::{ImplTraitPosition, ResolverAstLoweringExt};
3940

@@ -129,7 +130,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
129130
) -> &'hir hir::FnDecl<'hir> {
130131
let args_count = if let Some(local_sig_id) = sig_id.as_local() {
131132
// Map may be filled incorrectly due to recursive delegation.
132-
// Error will be emmited later in astconv.
133+
// Error will be emitted later during HIR ty lowering.
133134
self.resolver.fn_parameter_counts.get(&local_sig_id).cloned().unwrap_or_default()
134135
} else {
135136
self.tcx.fn_arg_names(sig_id).len()

compiler/rustc_ast_lowering/src/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1427,8 +1427,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
14271427
// Error if `?Trait` bounds in where clauses don't refer directly to type parameters.
14281428
// Note: we used to clone these bounds directly onto the type parameter (and avoid lowering
14291429
// these into hir when we lower thee where clauses), but this makes it quite difficult to
1430-
// keep track of the Span info. Now, `add_implicitly_sized` in `AstConv` checks both param bounds and
1431-
// where clauses for `?Sized`.
1430+
// keep track of the Span info. Now, `<dyn HirTyLowerer>::add_implicit_sized_bound`
1431+
// checks both param bounds and where clauses for `?Sized`.
14321432
for pred in &generics.where_clause.predicates {
14331433
let WherePredicate::BoundPredicate(bound_pred) = pred else {
14341434
continue;

compiler/rustc_hir/src/hir.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,6 @@ pub enum TraitBoundModifier {
428428
MaybeConst,
429429
}
430430

431-
/// The AST represents all type param bounds as types.
432-
/// `typeck::collect::compute_bounds` matches these against
433-
/// the "special" built-in traits (see `middle::lang_items`) and
434-
/// detects `Copy`, `Send` and `Sync`.
435431
#[derive(Clone, Copy, Debug, HashStable_Generic)]
436432
pub enum GenericBound<'hir> {
437433
Trait(PolyTraitRef<'hir>, TraitBoundModifier),
@@ -1860,7 +1856,7 @@ pub enum ExprKind<'hir> {
18601856
/// Wraps the expression in a terminating scope.
18611857
/// This makes it semantically equivalent to `{ let _t = expr; _t }`.
18621858
///
1863-
/// This construct only exists to tweak the drop order in HIR lowering.
1859+
/// This construct only exists to tweak the drop order in AST lowering.
18641860
/// An example of that is the desugaring of `for` loops.
18651861
DropTemps(&'hir Expr<'hir>),
18661862
/// A `let $pat = $expr` expression.
@@ -2293,7 +2289,7 @@ pub enum ImplItemKind<'hir> {
22932289
/// Bind a type to an associated type (i.e., `A = Foo`).
22942290
///
22952291
/// Bindings like `A: Debug` are represented as a special type `A =
2296-
/// $::Debug` that is understood by the astconv code.
2292+
/// $::Debug` that is understood by the HIR ty lowering code.
22972293
///
22982294
/// FIXME(alexreg): why have a separate type for the binding case,
22992295
/// wouldn't it be better to make the `ty` field an enum like the

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+46-57
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use crate::bounds::Bounds;
1717
use crate::errors;
1818

1919
impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20-
/// Sets `implicitly_sized` to true on `Bounds` if necessary
20+
/// Add a `Sized` bound to the `bounds` if appropriate.
21+
///
22+
/// Doesn't add the bound if the HIR bounds contain any of `Sized`, `?Sized` or `!Sized`.
2123
pub(crate) fn add_sized_bound(
2224
&self,
2325
bounds: &mut Bounds<'tcx>,
@@ -101,21 +103,27 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
101103
}
102104
}
103105

104-
/// This helper takes a *converted* parameter type (`param_ty`)
105-
/// and an *unconverted* list of bounds:
106+
/// Lower HIR bounds into `bounds` given the self type `param_ty` and the overarching late-bound vars if any.
107+
///
108+
/// ### Examples
106109
///
107-
/// ```text
108-
/// fn foo<T: Debug>
109-
/// ^ ^^^^^ `ast_bounds` parameter, in HIR form
110-
/// |
111-
/// `param_ty`, in ty form
110+
/// ```ignore (illustrative)
111+
/// fn foo<T>() where for<'a> T: Trait<'a> + Copy {}
112+
/// // ^^^^^^^ ^ ^^^^^^^^^^^^^^^^ `ast_bounds`, in HIR form
113+
/// // | |
114+
/// // | `param_ty`, in ty form
115+
/// // `bound_vars`, in ty form
116+
///
117+
/// fn bar<T>() where T: for<'a> Trait<'a> + Copy {} // no overarching `bound_vars` here!
118+
/// // ^ ^^^^^^^^^^^^^^^^^^^^^^^^ `ast_bounds`, in HIR form
119+
/// // |
120+
/// // `param_ty`, in ty form
112121
/// ```
113122
///
114-
/// It adds these `ast_bounds` into the `bounds` structure.
123+
/// ### A Note on Binders
115124
///
116-
/// **A note on binders:** there is an implied binder around
117-
/// `param_ty` and `ast_bounds`. See `instantiate_poly_trait_ref`
118-
/// for more details.
125+
/// There is an implied binder around `param_ty` and `ast_bounds`.
126+
/// See `lower_poly_trait_ref` for more details.
119127
#[instrument(level = "debug", skip(self, ast_bounds, bounds))]
120128
pub(crate) fn lower_poly_bounds<'hir, I: Iterator<Item = &'hir hir::GenericBound<'tcx>>>(
121129
&self,
@@ -170,22 +178,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
170178
}
171179
}
172180

173-
/// Translates a list of bounds from the HIR into the `Bounds` data structure.
174-
/// The self-type for the bounds is given by `param_ty`.
181+
/// Lower HIR bounds into `bounds` given the self type `param_ty` and *no* overarching late-bound vars.
175182
///
176-
/// Example:
183+
/// ### Example
177184
///
178185
/// ```ignore (illustrative)
179-
/// fn foo<T: Bar + Baz>() { }
186+
/// fn foo<T: Bar + Baz>() {}
180187
/// // ^ ^^^^^^^^^ ast_bounds
181188
/// // param_ty
182189
/// ```
183-
///
184-
/// The `sized_by_default` parameter indicates if, in this context, the `param_ty` should be
185-
/// considered `Sized` unless there is an explicit `?Sized` bound. This would be true in the
186-
/// example above, but is not true in supertrait listings like `trait Foo: Bar + Baz`.
187-
///
188-
/// `span` should be the declaration size of the parameter.
189190
pub(crate) fn lower_mono_bounds(
190191
&self,
191192
param_ty: Ty<'tcx>,
@@ -227,12 +228,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
227228
bounds
228229
}
229230

230-
/// Given an HIR binding like `Item = Foo` or `Item: Foo`, pushes the corresponding predicates
231-
/// onto `bounds`.
231+
/// Lower an associated item binding from HIR into `bounds`.
232+
///
233+
/// ### A Note on Binders
232234
///
233-
/// **A note on binders:** given something like `T: for<'a> Iterator<Item = &'a u32>`, the
234-
/// `trait_ref` here will be `for<'a> T: Iterator`. The `binding` data however is from *inside*
235-
/// the binder (e.g., `&'a u32`) and hence may reference bound regions.
235+
/// Given something like `T: for<'a> Iterator<Item = &'a u32>`,
236+
/// the `trait_ref` here will be `for<'a> T: Iterator`.
237+
/// The `binding` data however is from *inside* the binder
238+
/// (e.g., `&'a u32`) and hence may reference bound regions.
236239
#[instrument(level = "debug", skip(self, bounds, dup_bindings, path_span))]
237240
pub(super) fn lower_assoc_item_binding(
238241
&self,
@@ -244,22 +247,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
244247
path_span: Span,
245248
only_self_bounds: OnlySelfBounds,
246249
) -> Result<(), ErrorGuaranteed> {
247-
// Given something like `U: SomeTrait<T = X>`, we want to produce a
248-
// predicate like `<U as SomeTrait>::T = X`. This is somewhat
249-
// subtle in the event that `T` is defined in a supertrait of
250-
// `SomeTrait`, because in that case we need to upcast.
251-
//
252-
// That is, consider this case:
253-
//
254-
// ```
255-
// trait SubTrait: SuperTrait<i32> { }
256-
// trait SuperTrait<A> { type T; }
257-
//
258-
// ... B: SubTrait<T = foo> ...
259-
// ```
260-
//
261-
// We want to produce `<B as SuperTrait<i32>>::T == foo`.
262-
263250
let tcx = self.tcx();
264251

265252
let assoc_kind = if binding.gen_args.parenthesized
@@ -272,6 +259,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
272259
ty::AssocKind::Type
273260
};
274261

262+
// Given something like `U: Trait<T = X>`, we want to produce a predicate like
263+
// `<U as Trait>::T = X`.
264+
// This is somewhat subtle in the event that `T` is defined in a supertrait of `Trait`,
265+
// because in that case we need to upcast. I.e., we want to produce
266+
// `<B as SuperTrait<i32>>::T == X` for `B: SubTrait<T = X>` where
267+
//
268+
// trait SubTrait: SuperTrait<i32> {}
269+
// trait SuperTrait<A> { type T; }
275270
let candidate = if self.probe_trait_that_defines_assoc_item(
276271
trait_ref.def_id(),
277272
assoc_kind,
@@ -449,6 +444,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
449444
span: binding.span,
450445
}));
451446
}
447+
// Lower an equality constraint like `Item = u32` as found in HIR bound `T: Iterator<Item = u32>`
448+
// to a projection predicate: `<T as Iterator>::Item = u32`.
452449
hir::TypeBindingKind::Equality { term } => {
453450
let term = match term {
454451
hir::Term::Ty(ty) => self.lower_ty(ty).into(),
@@ -490,29 +487,21 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
490487
},
491488
);
492489

493-
// "Desugar" a constraint like `T: Iterator<Item = u32>` this to
494-
// the "projection predicate" for:
495-
//
496-
// `<T as Iterator>::Item = u32`
497490
bounds.push_projection_bound(
498491
tcx,
499492
projection_ty
500493
.map_bound(|projection_ty| ty::ProjectionPredicate { projection_ty, term }),
501494
binding.span,
502495
);
503496
}
497+
// Lower a constraint like `Item: Debug` as found in HIR bound `T: Iterator<Item: Debug>`
498+
// to a bound involving a projection: `<T as Iterator>::Item: Debug`.
504499
hir::TypeBindingKind::Constraint { bounds: ast_bounds } => {
505-
// "Desugar" a constraint like `T: Iterator<Item: Debug>` to
506-
//
507-
// `<T as Iterator>::Item: Debug`
508-
//
509-
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
510-
// parameter to have a skipped binder.
511-
//
512-
// NOTE: If `only_self_bounds` is true, do NOT expand this associated
513-
// type bound into a trait predicate, since we only want to add predicates
514-
// for the `Self` type.
500+
// NOTE: If `only_self_bounds` is true, do NOT expand this associated type bound into
501+
// a trait predicate, since we only want to add predicates for the `Self` type.
515502
if !only_self_bounds.0 {
503+
// Calling `skip_binder` is okay, because `lower_bounds` expects the `param_ty`
504+
// parameter to have a skipped binder.
516505
let param_ty = Ty::new_alias(tcx, ty::Projection, projection_ty.skip_binder());
517506
self.lower_poly_bounds(
518507
param_ty,

compiler/rustc_hir_analysis/src/astconv/generics.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -143,35 +143,33 @@ fn generic_arg_mismatch_err(
143143
err.emit()
144144
}
145145

146-
/// Creates the relevant generic arguments
147-
/// corresponding to a set of generic parameters. This is a
148-
/// rather complex function. Let us try to explain the role
146+
/// Lower generic arguments from the HIR to the [`rustc_middle::ty`] representation.
147+
///
148+
/// This is a rather complex function. Let us try to explain the role
149149
/// of each of its parameters:
150150
///
151-
/// To start, we are given the `def_id` of the thing whose generic
152-
/// parameters we are instantiating, and a partial set of
153-
/// arguments `parent_args`. In general, the generic arguments
154-
/// for an item begin with arguments for all the "parents" of
155-
/// that item -- e.g., for a method it might include the
156-
/// parameters from the impl.
151+
/// To start, we are given the `def_id` of the thing whose generic parameters we
152+
/// are creating, and a partial set of arguments `parent_args`. In general,
153+
/// the generic arguments for an item begin with arguments for all the "parents"
154+
/// of that item -- e.g., for a method it might include the parameters from the impl.
157155
///
158156
/// Therefore, the method begins by walking down these parents,
159157
/// starting with the outermost parent and proceed inwards until
160158
/// it reaches `def_id`. For each parent `P`, it will check `parent_args`
161159
/// first to see if the parent's arguments are listed in there. If so,
162-
/// we can append those and move on. Otherwise, it invokes the
163-
/// three callback functions:
160+
/// we can append those and move on. Otherwise, it uses the provided
161+
/// [`GenericArgsLowerer`] `ctx` which has the following methods:
164162
///
165163
/// - `args_for_def_id`: given the `DefId` `P`, supplies back the
166164
/// generic arguments that were given to that parent from within
167165
/// the path; so e.g., if you have `<T as Foo>::Bar`, the `DefId`
168166
/// might refer to the trait `Foo`, and the arguments might be
169167
/// `[T]`. The boolean value indicates whether to infer values
170168
/// for arguments whose values were not explicitly provided.
171-
/// - `provided_kind`: given the generic parameter and the value from `args_for_def_id`,
172-
/// instantiate a `GenericArg`.
173-
/// - `inferred_kind`: if no parameter was provided, and inference is enabled, then
174-
/// creates a suitable inference variable.
169+
/// - `provided_kind`: given the generic parameter and the value
170+
/// from `args_for_def_id`, creating a `GenericArg`.
171+
/// - `inferred_kind`: if no parameter was provided, and inference
172+
/// is enabled, then creates a suitable inference variable.
175173
pub fn lower_generic_args<'tcx: 'a, 'a>(
176174
tcx: TyCtxt<'tcx>,
177175
def_id: DefId,

0 commit comments

Comments
 (0)