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

Fallback to Hash for Mustermann::EqualityMap produces warnings and seems incorrect #89

Closed
eregon opened this issue Jul 9, 2018 · 3 comments
Assignees

Comments

@eregon
Copy link
Contributor

eregon commented Jul 9, 2018

Mustermann::EqualityMap falls back to Hash if ObjectSpace::WeakMap is not defined.

def self.new
defined?(ObjectSpace::WeakMap) ? super : {}
end

However, this fallback doesn't seem correct as then

@map ||= EqualityMap.new
@map.fetch(string, options) { super(string, options) { options } }

ends up calling Hash#fetch with both a default value argument and a block.
This produces the warning:

.../gems/mustermann-1.0.2/lib/mustermann/pattern.rb:59: warning: block supersedes default value argument

And it means the second argument options is therefore ignored.

Mustermann::EqualityMap#fetch, used only if ObjectSpace::WeakMap is defined, has a different signature:

I guess one easy fix is to define it like fetch(key) and in pattern.rb call it like

@map.fetch([string, options]) { super(string, options) { options } }

See oracle/truffleruby#1375 (comment) for the original report in TruffleRuby.

@eregon
Copy link
Contributor Author

eregon commented Aug 4, 2018

@namusyaka Hello, could you take a look at this issue?

@namusyaka
Copy link
Member

@eregon Your fix looks reasonable to me. Could you send a patch?

@eregon
Copy link
Contributor Author

eregon commented Aug 5, 2018

@namusyaka Thank you for taking a look, I'll make a PR.

eregon added a commit to eregon/mustermann that referenced this issue Aug 6, 2018
* Fixes sinatra#89.
* Add specs to prevent regressions.
* #dup the key if needed as ObjectSpace::WeakMap requires
  non-frozen key and value:

$ ruby -v (also happens on MRI 2.5.1)
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
$ ruby -e 'ObjectSpace::WeakMap.new["foo"] = "bar"'
$ ruby -e 'ObjectSpace::WeakMap.new["foo".freeze] = "bar"'
-e:1:in `[]=': can't modify frozen String (RuntimeError)
$ ruby -e 'ObjectSpace::WeakMap.new["foo"] = "bar".freeze'
-e:1:in `[]=': can't modify frozen String (RuntimeError)
eregon added a commit to eregon/mustermann that referenced this issue Aug 6, 2018
* Fixes sinatra#89.
* Add specs to prevent regressions.
* #dup the key if needed as ObjectSpace::WeakMap requires
  non-frozen key and value:

$ ruby -v (also happens on MRI 2.5.1)
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
$ ruby -e 'ObjectSpace::WeakMap.new["foo"] = "bar"'
$ ruby -e 'ObjectSpace::WeakMap.new["foo".freeze] = "bar"'
-e:1:in `[]=': can't modify frozen String (RuntimeError)
$ ruby -e 'ObjectSpace::WeakMap.new["foo"] = "bar".freeze'
-e:1:in `[]=': can't modify frozen String (RuntimeError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants