E56A
Skip to content

fixes struct contract violation message#2999

Open
tommymchugh wants to merge 1 commit intoracket:masterfrom
tommymchugh:struct-contract-violation
Open

fixes struct contract violation message#2999
tommymchugh wants to merge 1 commit intoracket:masterfrom
tommymchugh:struct-contract-violation

Conversation

@tommymchugh
Copy link
Copy Markdown
Contributor

Resolves #2998

Example:

#lang racket
(define-struct/contract s ([x integer?][y boolean?]))
(s 1 2)

Old Output:

$  racket ~/tmp.rkt
make-s: contract violation
  expected: boolean?
  given: 2
  in: the 2nd argument of
      (-> integer? boolean? symbol? any)
  contract from: (struct s)
  blaming: /Users/tommymchugh/test.rkt
   (assuming the contract is correct)
  at: /Users/tommymchugh/test.rkt

New Output:

$  racket ~/tmp.rkt
make-s: contract violation
  expected: boolean?
  given: 2
  in: the 2nd argument of
      (-> integer? boolean? any)
  contract from: make-s
  blaming: /Users/tommymchugh/test.rkt
   (assuming the contract is correct)
  at: /Users/tommymchugh/test.rkt

@tommymchugh tommymchugh changed the title fixed struct contract violation message fixes struct contract violation message Jan 5, 2020
(begin
(define-values () (begin auto-check ... (values)))
(define (guard super-field ... non-auto-name ... struct-name)
(define (guard-contract super-field ... non-auto-name ...)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there now any difference between this guard-contract and values?

The only differences I'm aware of are arity and name. The arity shouldn't matter here. The name shouldn't matter either, as the error messages come from the contract form which supplies the name with (quote maker).

So you should be able to get by without defining guard-contract, with

(define (guard super-field ... non-auto-name ... struct-name)
  ((contract (-> super-contract ... non-auto-contracts ... any)
             values
             ....)
   super-field ... non-auto-name ...))

One more minor question, I noticed you replaced (current-contract-region) with (quote maker). I don't think there's good reason to change it. Since it's a syntax parameter not a run-time parameter, it won't be affected by being inside the function body.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, made these changes, but this will probably change anyway with the new ->

@rfindler
Copy link
Copy Markdown
Member
rfindler commented Jan 6, 2020

It isn't a good idea to put the call to contract inside the guard; that does too much work to put done on each call.

I think the right approach here is to make (yet another) variation of -> that ignores the last argument when it produces the error message.

@rfindler
Copy link
Copy Markdown
Member
rfindler commented Jan 6, 2020

(also, of course, we need test cases!)

@tommymchugh
Copy link
Copy Markdown
Contributor Author

@rfindler Roger that, will work on that and update this PR.

@AlexKnauth
Copy link
Copy Markdown
Member

It isn't a good idea to put the call to contract inside the guard

So is it better to put the contract call outside, even when the guard calls that result function every time like this:

(define guarded-values
  (contract (-> super-contract ... non-auto-contracts ... any)
            values
            ....))
(define (guard super-field ... non-auto-name ... struct-name)
  (guarded-values super-field ... non-auto-name ...))

Is this okay, or is there a downside that I'm not seeing, which would make a new variation of -> better?

@rfindler
Copy link
Copy Markdown
Member
rfindler commented Jan 6, 2020

@AlexKnauth Oh, of course. That's better! Thanks.

@tommymchugh
Copy link
Copy Markdown
Contributor Author

That sounds great, just pushed those changes to this PR.

@tommymchugh tommymchugh requested a review from AlexKnauth January 7, 2020 15:17
@AlexKnauth
Copy link
Copy Markdown
Member
AlexKnauth commented Jan 8, 2020

I'm still wondering about the blame information.

The old positive-blame-expr was the (current-contract-region) within the with-contract #:region struct struct-name form, which in the old error message example was (struct s).

  contract from: (struct s)

However, the new positive-blame-expr is now (current-contract-region) outside of that with-contract, so in the new error message example it's more generic:

  contract from: /path/to/test.rkt

So to keep the positive blame party the same as it was before, the contract expression for guarded-values should be kept within the with-contract #:region struct struct-name form.

To do this I would put the definitions of both guarded-values and guard within the with-contract form just above the use of define-struct/derived.

@tommymchugh
Copy link
Copy Markdown
Contributor Author

@AlexKnauth That did the trick! Thanks so much for the advice. That was what I had noticed when I had initially changed the blame-id since it was outside the struct, but now there is no regression when the define is within the with-contract. Violation message is now exactly like previous version, just without the extra argument in the procedure. I just squashed and rebased, so looks pretty much good to go unless there are any other concerns I can address!

@rfindler
Copy link
Copy Markdown
Member
rfindler commented Jan 8, 2020

There are no new test cases and the new behavior certainly can be captured with tests.

@tommymchugh
Copy link
Copy Markdown
Contributor Author
tommymchugh commented Jan 8, 2020 via email

@rfindler
Copy link
Copy Markdown
Member
rfindler commented Jan 8, 2020

(Also we'll want a test case that captures the issue that @AlexKnauth noticed, too -- assuming there wasn't already one?)

@pmatos
Copy link
Copy Markdown
Collaborator
pmatos commented Apr 22, 2020

ping! Can we look at this again so it is not forgotten? There's an approved review by @AlexKnauth but no merge.

@rfindler
Copy link
Copy Markdown
Member

Looks to me like it isn't ready to merge based on the discussion, @pmatos .

@pmatos pmatos requested review from bennn and rfindler May 8, 2020 10:59
@tommymchugh
Copy link
Copy Markdown
Contributor Author

@pmatos @rfindler Sorry kinda lost track of this with all the craziness of school and such! I'll work on this and submit a patch ASAP! :)

@rfindler
Copy link
Copy Markdown
Member

No rush! Thanks for your contributions.

@shhyou shhyou added pr:forgotten No activity for six months: can't find a reviewer, no continued dev, stuck since no consensus component: contract the contract system: combinators, attachment forms, semantics, implementation, optimization labels Sep 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: contract the contract system: combinators, attachment forms, semantics, implementation, optimization pr:forgotten No activity for six months: can't find a reviewer, no continued dev, stuck since no consensus

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extra argument in contract error message

5 participants

0