Skip to content

Feed ErrorGuaranteed from early lifetime resolution errors through to bound variable resolution#152076

Open
eggyal wants to merge 2 commits intorust-lang:mainfrom
eggyal:undeclared-object-lifetime
Open

Feed ErrorGuaranteed from early lifetime resolution errors through to bound variable resolution#152076
eggyal wants to merge 2 commits intorust-lang:mainfrom
eggyal:undeclared-object-lifetime

Conversation

@eggyal
Copy link
Contributor

@eggyal eggyal commented Feb 3, 2026

Undeclared lifetimes that appear in user code always give rise to an error (E0261) early in name resolution. This patch adds such undeclared lifetimes to the relevant context's bound vars, so that they resolve to regions with RegionKind::Error(ErrorGuaranteed), in order to suppress some ensuing diagnostics (that might be resolved if the lifetime becomes properly declared).

Fixes #152014
r? fmease

Undeclared lifetimes that appear in user code always give rise to an
error (E0261) early in name resolution.  This patch adds such undeclared
lifetimes to the relevant context's bound vars, so that they resolve to
regions with `RegionKind::Error(ErrorGuaranteed)`, in order to suppress
some ensuing diagnostics (that might be resolved if the lifetime becomes
properly declared).
@rustbot
Copy link
Collaborator

rustbot commented Feb 3, 2026

Changes to the size of AST and/or HIR nodes.

cc @nnethercote

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 3, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 3, 2026

fmease is currently at their maximum review capacity.
They may take a while to respond.

Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! I won't be able to review this tonight but I'd like to note that thanks to your changes we should now be able to get rid of the minor hack in HIR ty lowering as I've mentioned in the issue:

if let hir::Node::Ty(hir::Ty {
kind: hir::TyKind::Ref(parent_lifetime, _),
..
}) = tcx.parent_hir_node(hir_id)
&& tcx.named_bound_var(parent_lifetime.hir_id).is_none()
{
// Parent lifetime must have failed to resolve. Don't emit a redundant error.
RegionInferReason::ExplicitObjectLifetime
} else {
RegionInferReason::ObjectLifetimeDefault
}

(by replacing that whole snippet with just RegionInferReason::ObjectLifetimeDefault)

View changes since this review

Copy link
Contributor Author

@eggyal eggyal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of points for the review to consider.

View changes since this review

@eggyal
Copy link
Contributor Author

eggyal commented Feb 3, 2026

I won't be able to review this tonight

No worries !

I'd like to note that thanks to your changes we should now be able to get rid of the minor hack in HIR ty lowering I've mentioned in the issue

Ah, good point! I forgot about that. I'll take a look and push an update if it can be removed cleanly.

@fmease
Copy link
Member

fmease commented Feb 3, 2026

Preliminary sanity perf check due to size changes.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Feb 3, 2026
Bind undeclared lifetimes as erroneous bound vars
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 3, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 4, 2026

☀️ Try build successful (CI)
Build commit: e81e54f (e81e54fb4fbe70e3d11975443d97b3652269a38d, parent: 366a1b93e7f466ebe559477add05f064873d0c71)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (e81e54f): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.1% [0.1%, 0.2%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 3.1%, secondary -0.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
3.1% [3.1%, 3.1%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.9% [-0.9%, -0.8%] 3
All ❌✅ (primary) 3.1% [3.1%, 3.1%] 1

Cycles

Results (secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.2% [3.2%, 3.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.0% [-3.0%, -3.0%] 1
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 472.721s -> 472.345s (-0.08%)
Artifact size: 397.76 MiB -> 397.70 MiB (-0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 4, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 4, 2026

HIR ty lowering was modified

cc @fmease

let span = *entry.get();
let err = ResolutionError::NameAlreadyUsedInParameterList(ident, span);
self.report_error(param.ident.span, err);
let guar = self.r.report_error(param.ident.span, err);
Copy link
Contributor Author

@eggyal eggyal Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will unconditionally emit an error, whereas before it was suppressed in rustdoc; however I'm not sure that such suppression was correct here anyway—see the comments on Self::should_report_error, which don't appear to apply:

/// If we're actually rustdoc then avoid giving a name resolution error for `cfg()` items or
// an invalid `use foo::*;` was found, which can cause unbounded amounts of "item not found"
// errors. We silence them all.

In any event, this change doesn't appear to be breaking any tests...

Copy link
Contributor Author

@eggyal eggyal Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, it wasn't breaking x test locally, but clearly it's broken one in CI.

@rustbot author

@eggyal eggyal force-pushed the undeclared-object-lifetime branch from e7d86b7 to a531436 Compare February 4, 2026 10:09
Copy link
Contributor Author

@eggyal eggyal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whereas in 08ed1a1 I was only feeding ErrorGuaranteed through to RBV in the specific case of undeclared named lifetimes, in a531436 I've extended the logic to feed through such an ErrorGuaranteed for every early lifetime resolution error. This enables the aforementioned "hack" to be removed without giving rise to new instances of E0228 ("the lifetime bound for this object type cannot be deduced from context"), however it does also suppress a few other errors that previously were emitted—see updates to test outputs.

View changes since this review

I didn't squash (yet) in case you had begun reviewing and it was easier to see what has since changed. However, these commits really ought to be squashed (with commensurate updates to the PR description and commit comments) before merging, and review may be easier combined in any event.

@eggyal eggyal changed the title Bind undeclared lifetimes as erroneous bound vars Feed ErrorGuaranteed from early lifetime resolution errors through to bound variable resolution Feb 4, 2026
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
REPOSITORY                                   TAG       IMAGE ID       CREATED      SIZE
ghcr.io/dependabot/dependabot-updater-core   latest    bcec0b4e062b   8 days ago   783MB
=> Removing docker images...
Deleted Images:
untagged: ghcr.io/dependabot/dependabot-updater-core:latest
untagged: ghcr.io/dependabot/dependabot-updater-core@sha256:b662be51f7b8ef7e2c8464428f14e49cb79c36aa9afb7ecb9221dfe0f507050c
deleted: sha256:bcec0b4e062b5ffe11cc1c2729558c0cd96621c0271ab5e97ff3a56e0c25045a
deleted: sha256:64e147d5e54d9be8b8aa322e511cda02296eda4b8b8d063c6a314833aca50e29
deleted: sha256:5cba409bb463f4e7fa1a19f695450170422582c1bc7c0e934d893b4e5f558bc6
deleted: sha256:cddc6ebd344b0111eaab170ead1dfda24acdfe865ed8a12599a34d338fa8e28b
deleted: sha256:2412c3f334d79134573cd45e657fb6cc0abd75bef3881458b0d498d936545c8d
---
tests/ui/double_parens.rs ... ok
tests/ui/drop_non_drop.rs ... ok
tests/ui/duplicate_underscore_argument.rs ... ok
tests/ui/duplicated_attributes.rs ... ok
tests/ui/duration_suboptimal_units_days_weeks.rs ... ok
tests/ui/duration_suboptimal_units.rs ... ok
tests/ui/duration_subsec.rs ... ok
tests/ui/double_parens.fixed ... ok
tests/ui/duration_suboptimal_units_days_weeks.fixed ... ok
tests/ui/duration_suboptimal_units.fixed ... ok
tests/ui/duration_subsec.fixed ... ok
tests/ui/elidable_lifetime_names.rs ... ok
tests/ui/else_if_without_else.rs ... ok
tests/ui/eager_transmute.rs ... ok
tests/ui/empty_docs.rs ... ok
---
...............................................    (147/147)

======== tests/rustdoc-gui/globals.goml ========

[ERROR] line 14: Error: The window properties still all match: for command `wait-for-window-property-false: {"searchIndex": null}`
    at <file:///checkout/obj/build/x86_64-unknown-linux-gnu/test/rustdoc-gui/doc/test_docs/index.html?search=Foo>

======== tests/rustdoc-gui/search-result-display.goml ========

[WARNING] line 39: Delta is 0 for "x", maybe try to use `compare-elements-position` instead?

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 5, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 5, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

We generally emit an additional error if a trait object lifetime bound defaults to an unresolved lifetime in item signatures

6 participants