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

Check the well-formed-ness of type aliases. #44075

Open
eddyb opened this issue Aug 24, 2017 · 3 comments
Open

Check the well-formed-ness of type aliases. #44075

eddyb opened this issue Aug 24, 2017 · 3 comments
Labels
A-typesystem Area: The type system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@eddyb
Copy link
Member

eddyb commented Aug 24, 2017

Currently, these compile:

struct Foo<T: Copy>(T);
type X = Foo<String>;
type Y = <() as Iterator>::Item;

Using them does produce an error at the use site, but not in the definition.

@eddyb eddyb added A-typesystem Area: The type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 24, 2017
@eddyb
Copy link
Member Author

eddyb commented Aug 24, 2017

If we do check WF for type aliases, can we also easily check if there's bounds not used to satisfy well-formed-ness rules? Because those bounds we current ignore where the type alias is used.
cc @nikomatsakis @arielb1

@nikomatsakis
Copy link
Contributor

@eddyb not super easily

@eddyb
Copy link
Member Author

eddyb commented Sep 9, 2018

For the record, it's actually easy. This is what it looks like on the current compiler:

let user_predicates = fcx.param_env.caller_bounds;
let wf_predicates = ty::wf::obligations(
    &fcx.infcx,
    fcx.param_env,
    fcx.body_id,
    item_ty,
    item.span,
).into_iter().flatten().map(|o| o.predicate).collect();
let wf_predicates = traits::elaborate_predicates(fcx.tcx, wf_predicates);
let wf_param_env = ty::ParamEnv {
    caller_bounds: fcx.tcx.mk_predicates(wf_predicates),
    reveal: fcx.param_env.reveal,
};
fcx.register_predicates(user_predicates.iter().map(|&predicate| {
    let code = ObligationCauseCode::MiscObligation;
    let cause = traits::ObligationCause::new(item.span, fcx.body_id, code);
    traits::Obligation::new(cause, wf_param_env, predicate)
}));

Of course, proper reporting is more work, but it's a shame we didn't figure out we can do these checks, in the first place, last year, instead of so close to the edition release (#49441 (comment)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants